Simple dependency injection library for .NET 6
What is it?
It is a minimal dependency injection library to register all of your dependencies.
Features
- Two object lifetimes: transient and singleton;
- Convenient methods to register singleton or transient dependencies;
- Transient dependency implementation can be replaced;
- Singletons and the service itself uses lazy loading to save memory;
Usage
Doxygen documentation
Here are some typical scenarios when using Dependapult to show the useage of the service.
Getting Dependapult service object
DependapultService service = DependapultService.Instance;
Registering a dependency without providing arguments
service.RegisterSingleton<IDependency, Dependency>();
service.RegisterTransient<IDependency, Dependency>();
DependencyLifetime
Lifetime of a dependency object.
Definition: DependencyLifetime.cs:10
Registering a dependency and providing arguments
object[] arguments = new object[] { "config", 150, false };
service.RegisterDependency<IDependency, Dependency>(
DependencyLifetime.Singleton, arguments);
service.RegisterSingleton<IDependency, Dependency>(arguments);
service.RegisterTransient<IDependency, Dependency>(arguments);
Registering a dependency and providing a creator function
service.RegisterDependency<IDependency>(
DependencyLifetime.Singleton, s =>
new Dependency(s.GetDependency<IOtherDependency>(), 200));
service.RegisterSingleton<IDependency>(s => new Dependency(s.GetDependency<IOtherDependency>(), 200));
service.RegisterTransient<IDependency>(s => new Dependency(s.GetDependency<IOtherDependency>(), 200));
Replacing a dependency
service.RegisterTransient<IDependency, Dependency>(true);
Getting a dependency
IDependency dependency = service.GetDependency<IDependency>();
IDependency dependency = service.GetDependency(typeof(IDependency)) as IDependency;
Tagging a constructor
If your dependency contains multiple public constructor you must add a DependapultConstructor
attribute to the constructor you want to be used by the service.
class Dependency
{
public Dependency()
{
...
}
[DependapultConstructor]
public Dependency(IOtherDependency otherDependency)
{
...
}
}