# Slim ## Service lifetime manager and dependency injection framework Uses lazy approach and only implements objects when called. Supports multiple Register calls without needing to rebuild. Supports scoped lifetime of services. To use, simply create and register types: ```c# var serviceManager = new ServiceManager(); serviceManager.RegisterSingleton(); serviceManager.RegisterSingleton(sp => new Service2(sp.GetService()); serviceManager.GetService(); ``` To manage the lifetime of the objects, use ``` serviceManager.RegisterSingleton ```, ``` serviceManager.RegisterTransient ``` or ``` serviceManager.RegisterScoped ```. Can register a services for all interaces it implements: ```c# serviceManager.RegisterSingleton(registerAllInterfaces: true); serviceManager.GetService(); serviceManager.GetService(); ``` Otherwise, calling ```c# serviceManager.RegisterSingleton(); ``` Will only register Service1 as Service1. Supports both generics or arguments as types: ```c# serviceManager.RegisterTransient(); serviceManager.RegisterTransient(typeof(IService2), typeof(Service2)); ``` Can register current service manager as a valid dependency: ```c# serviceManager.RegisterServiceManager(); ``` Can handle multiple types of exceptions: ```c# serviceManager.HandleException((sp, ex) => DoSomething1(ex)); serviceManager.HandleException((sp, ex) => DoSomething2(ex)); serviceManager.HandleException((sp, ex) => DoSomething3(ex)); ``` In case of reinitialization, call: ```c# serviceManager.Clear(); ``` ``` serviceManager.Clear ``` calls ``` IDisposable.Dispose ``` on singletons that implement ``` IDisposable ```. To build all singletons currently registered, call: ```c# serviceManager.BuildSingletons(); ``` `IDependencyResolver` interface to implement manual resolvers for special cases. Register on the `IServiceManager` using: ```c# serviceManager.RegisterResolver(new SomeDependencyResolver()); ``` To create a scope, call the following method and use the new instance of scoped IServiceProvider. ```c# serviceManager.CreateScope(); ``` By default, the returned scope manager will be readonly. This means, calling ```c# scopedServiceManager.RegisterService(); ``` will throw an InvalidOperationException. To override the functionality from above, set the `AllowScopedManagerModifications` property to true in the original service manager before creating the scoped service manager ```c# serviceManager.AllowScopedManagerModifications = true; var scopedServiceManager = serviceManager.CreateScope(); scopedServiceManager.RegisterService(); ``` This way, no exception is thrown. To retrieve all services of a certain type from the service manager, use ```c# serviceManager.GetServicesOfType(); ``` This will return an `IEnumerable` with all the services which implement the `ISharedInterface`. In order to give priority to certain constructors in a class, mark the constructors with `[PreferredConstructor]` attribute. `ServiceManager` will prioritize those constructors.