Dependapult 1.0.1
dotnet add package Dependapult --version 1.0.1
NuGet\Install-Package Dependapult -Version 1.0.1
<PackageReference Include="Dependapult" Version="1.0.1" />
paket add Dependapult --version 1.0.1
#r "nuget: Dependapult, 1.0.1"
// Install Dependapult as a Cake Addin
#addin nuget:?package=Dependapult&version=1.0.1
// Install Dependapult as a Cake Tool
#tool nuget:?package=Dependapult&version=1.0.1
Dependapult
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;
You can view the changelog here.
Usage
Here are some typical scenarios when using Dependapult to show the useage of the service.
Getting Dependapult service object
First step when using the component is to get the DependapultService
object which contains all of the relevant methods.
DependapultService service = DependapultService.Instance;
Registering a dependency without providing arguments
Most typical usage of the service is to provide the lifetime of the object, interface and the implementor types.
// Manually provide lifetime
service.RegisterDependency<IDependency, Dependency>(DependencyLifetime.Singleton);
// Singleton convenience method
service.RegisterSingleton<IDependency, Dependency>();
// Transient convenience method
service.RegisterTransient<IDependency, Dependency>();
Registering a dependency and providing arguments
A niche use case where you can additionaly provide an array of arguments to pass to the constructor.
object[] arguments = new object[] { "config", 150, false };
// Manually provide lifetime
service.RegisterDependency<IDependency, Dependency>(DependencyLifetime.Singleton, arguments);
// Singleton convenience method
service.RegisterSingleton<IDependency, Dependency>(arguments);
// Transient convenience method
service.RegisterTransient<IDependency, Dependency>(arguments);
Registering a dependency and providing a creator function
It is possible to provide a creator function which creates a dependecy object. The creator function receives a DependencyService
object and returns a dependency.
This is meant for cases where a constructor expects primitive types or if the dependecy has multiple public constructor.
// Manually provide lifetime
service.RegisterDependency<IDependency>(DependencyLifetime.Singleton, s => new Dependency(s.GetDependency<IOtherDependency>(), 200));
// Singleton convenience method
service.RegisterSingleton<IDependency>(s => new Dependency(s.GetDependency<IOtherDependency>(), 200));
// Transient convenience method
service.RegisterTransient<IDependency>(s => new Dependency(s.GetDependency<IOtherDependency>(), 200));
Replacing a dependency
It is possible to replace dependencies. Only transient dependencies can be replaced. Already created objects are unaffected.
// Manually provide lifetime
service.RegisterDependency<IDependency, Dependency>(DependencyLifetime.Transient, true);
// Transient convenience method
service.RegisterTransient<IDependency, Dependency>(true);
Getting a dependency
The final step of the service is to get the dependency that you need. It is recommended to use the method with generics.
// With generics
IDependency dependency = service.GetDependency<IDependency>();
// Without generics
IDependency dependency = service.GetDependency(typeof(IDependency)) as IDependency;
Tagging a constructor
If your dependency contains multiple public constructors you must add a DependapultConstructor
attribute to the constructor you want to be used by the service.
The attribute is not needed if the depencency has only one public constructor.
class Dependency
{
public Dependency()
{
...
}
[DependapultConstructor]
public Dependency(IOtherDependency otherDependency)
{
...
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Changelog.md