Dependency injection library for .NET applications.
- Create container
var container = ContainerFactory.Create();
-
Register dependency in container and specify object lifetime
- Register object
container.RegisterInstance<IVehicle>(new Vehicle());
- Register type
container.RegisterImplementation<IVehicle, Vehicle>(Lifetime.PerCall);
- Register factory
container.RegisterFactory<IVehicle>(c => new Vehicle(), Lifetime.PerContainer);
-
Get object from container
var vehicle = container.Get<IVehicle>();
-
Use container auto injection by applying [Inject] attribute.
- Inject using constructor
public class Vehicle : IVehicle { [Inject] public Vehicle(IMake make) { Make = make; } public IMake Make { get; } }
- Inject using property
public class Vehicle : IVehicle { [Inject] public IMake Make { get; set; } }
-
Inject container itself if required by using IReadOnlyContainer or IContainer interfaces
public class Vehicle : IVehicle
{
[Inject]
public Vehicle(IReadOnlyContainer container)
{
Container = container;
}
public IReadOnlyContainer Container { get; }
}