A dependency injection library with no features. The lack of features is the main feature!
- Concrete types with one constructor are resolved by default.
- One public constructor.
- Params parameter not supported.
- Class.
- Everything from the container is singleton.
- Constructor injection only unless providing custom factory functions.
- Require explicit bindings for interfaces and abstract types. Only one binding per type.
- Bind not allowed after resolving. Throws exception.
- Rebind shipped in a separate nuget to enforce that it is only used by tests.
- Dispose all things created by the container.
usingvarkernel=newKernel();varroot=kernel.Get<RootType>();usingvarkernel=newKernel().Bind<IFoo,Foo>();varroot=kernel.Get<RootType>();Note that:
usingvarkernel=newKernel().Bind<IFoo,Foo>().Bind<IFoo,Bar>();// throws binding IFoo a second time.Also note:
usingvarkernel=newKernel();varroot=kernel.Get<RootType>();kernal.Bind<Foo>(()=>newFoo());// throws binding after Get.usingvarkernel=newKernel().Bind(()=>Foo.Create());// this instance is disposed when the container is disposedvarroot=kernel.Get<RootType>();kernel.Bind<IFoo,Foo>();// throws as we did Get<RootType> meaning bind no longer allowed.Also allowed:
usingvarkernel=newKernel().Bind<IFoo>(()=>Foo.Create());// this instance is disposed when the container is disposedvarroot=kernel.Get<RootType>();usingvarkernel=newKernel().Bind(c =>Foo.Create(c.Get<Bar>()));// this instance is disposed when the container is disposedvarroot=kernel.Get<RootType>();Also allowed:
usingvarkernel=newKernel().Bind<IFoo>(c =>Foo.Create(c.Get<Bar>()));// this instance is disposed when the container is disposedvarroot=kernel.Get<RootType>();usingvarkernel=newKernel().Bind(Foo.Instance);// this instance is not disposed when the container is disposedvarroot=kernel.Get<RootType>();Also allowed:
usingvarkernel=newKernel().Bind<IFoo>(newFoo());// this instance is not disposed when the container is disposedNotifies before the container creates an instance. The event arguments contains the type that is about to be created.
Notifies after the container created an instance. The event arguments contains the instance that was created.
Notifies after calling kernel.Dispose() before an instance is removed. The event arguments contains the instance.
This can be used if there is additional cleanup needed.
Note that it is called for all instances not just for types that implement IDisposable
Creating a container and resolving a graph with 50 types. https://github.com/GuOrg/Gu.Inject/blob/master/Gu.Inject.Benchmarks/Benchmarks/GetGraph50.cs
| Method | Mean | Ratio | Allocated |
|------------------ |------------:|-------:|----------:|
| Ninject | 7,748.16 μs | 365.83 | 239.35 KB |
| SimpleInjector | 6,592.05 μs | 296.92 | 202.58 KB |
| DryIoc | 182.59 μs | 8.25 | 73.82 KB |
| ServiceCollection | 41.37 μs | 1.86 | 20.68 KB |
| Gu.Inject | 22.14 μs | 1.00 | 8.11 KB |