-
-
Notifications
You must be signed in to change notification settings - Fork 9
Simplify.DI
Decouples users and frameworks (that are based on Simplify.DI) from dependency on IOC containers. Instead of that, they will only depend on Simplify.DI interface.
Disciplines and unifies dependencies registration, verification and objects creation.
Provides DIContainer.Current
ambient context as centralized IOC container.
Provides unified Interface for creation IOC container providers (wrappers) for IOC container frameworks.
Using DIContainer.Current
and respective container provider you can switch between any IOC container without rewriting your code and IOC container behavior change.
Also as a centralized IOC container you can use it for building, for example, some framework which needs to use constructor injection and at the same time use it by that framework user (example using it in framework: registration, resolving with lifetime scope).
Available at NuGet as binary package
By default DIContainer.Current
initialized with DryIocDIProvider
container provider (DryIOC).
Another container providers available:
- Simplify.DI.Provider.SimpleInjector (SimpleInjector), Available at NuGet as binary package
- Simplify.DI.Provider.CastleWindsor (CastleWindsor), Available at NuGet as binary package
- Simplify.DI.Provider.Microsoft.Extensions.DependencyInjection (Dependency injection in ASP.NET Core), Available at NuGet as binary package
- Simplify.DI.Provider.Autofac (Autofac), Available at NuGet as binary package
Please create an issue, if you want another container provider.
Main IOC container interface is IDIContainerProvider
public interface IFoo
{
}
public class Foo : IFoo
{
public Foo(IBar bar)
{
}
}
public class Foo2 : IFoo
{
public Foo2(IBar bar, string someParameter)
{
}
}
public interface IBar
{
}
public class Bar : IBar
{
}
public class Bar2 : IBar
{
public Bar2(string someParameter)
{
}
}
// Simple registration
DIContainer.Current.Register<IBar, Bar>()
.Register<IFoo, Foo>();
// Registration with delegate
DIContainer.Current.Register<IBar>(p => new Bar2("test"));
// Registration with delegate and type resolve
DIContainer.Current.Register<IFoo>(p => new Foo2(p.Resolve<IBar>(), "test"));
var myObj = DIContainer.Current.Resolve<IBar>();
Container verification is a process of building registered objects graph and checking what all required types are present in a container and there is no registrations lifetime mismatches
DIContainer.Current.Verify();
In case of any container mismatches an exception will be thrown, exact type of the exception and messages will depend on current container provider
3 type of scopes available:
- PerLifetimeScope (default scope) - only one instance of the same type will be created inside opened scope
- Singleton - only one instance will be created per container
- Transient - new instance will be created on every Resolve request
// Simple registration
DIContainer.Current.Register<IBar, Bar>(LifetimeType.PerLifetimeScope);
// Any dependency while registration with delegate should be resolved with delegate parameter `p`, it is current scope resolve provider.
DIContainer.Current.Register<IFoo>(p => new Foo2(p.Resolve<IBar>(), "Test"), LifetimeType.PerLifetimeScope);
// Note, what scope.Container actually it is the same container as DIContainer.Current, for example, if using SimpleInjector, but with DryIoc it will be child container.
using (var scope = DIContainer.Current.BeginLifetimeScope())
{
var myObject = scope.Resolver.Resolve<IFoo>();
}
You can use any IOC container to directly register types or perform specific actions, for example, with Simple Injector:
var provider = new SimpleInjectorDIProvider();
DIContainer.Current = provider;
provider.Container.RegisterSingleton<IFoo>();
...
var provider = new SimpleInjectorDIProvider();
DIContainer.Current = provider;
...
DIContainer.Current.Register<IBar, Bar>();
...
provider.Container.Verify();
public static class Program
{
public static void Main(string[] args)
{
var provider = new DryIocDIProvider();
provider.RegisterAll()
.Verify();
using var scope = provider.BeginLifetimeScope();
scope.Resolver.Resolve<IMyService1>().DoWork();
}
private static IDIContainerProvider RegisterAll(this IDIContainerProvider provider)
{
provider.RegisterServicesGroup1()
.RegisterServicesGroup2();
return provider;
}
private static IDIRegistrator RegisterServicesGroup1(this IDIRegistrator registrator) => registrator
.Register<IMyService1, MyService1>()
.Register<IMyService2>(r => new MyService2("string parameter example"))
.Register<IMyService3>(r => new MyService3(r.Resolve<IMyService2>()));
private static IDIRegistrator RegisterServicesGroup2(this IDIRegistrator registrator) => registrator
.Register<IMySettings, MySettings>(LifetimeType.Singleton)
.Register<IUsersRepository, UsersRepository>()
.Register<IGroupsRepository, GroupsRepository>();
}
-
How to replace 'Simplify.DI.Provider.Microsoft.Extensions.DependencyInjection' internal container with container from AspNetCore application (This allows us to perform registrations via Simplify.DI keeping original container in AspNetCore application)