di-lite
1.0.0
See the version list below for details.
dotnet add package di-lite --version 1.0.0
NuGet\Install-Package di-lite -Version 1.0.0
<PackageReference Include="di-lite" Version="1.0.0" />
paket add di-lite --version 1.0.0
#r "nuget: di-lite, 1.0.0"
// Install di-lite as a Cake Addin #addin nuget:?package=di-lite&version=1.0.0 // Install di-lite as a Cake Tool #tool nuget:?package=di-lite&version=1.0.0
DiLite /dɪˈlʌɪt/ is a minimalist, lightweight DI framework. Registrations and resolutions will look very familiar to those accustomed to Autofac.
Usage
Create a ContainerBuilder
, make the registrations, then build your Container
:
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<MyClass>().As<IMyClass>();
var container = containerBuilder.Build();
Then simply resolve what you need from the Container
:
var myInstance = container.Resolve<IMyClass>();
Features
1. Single instance
Each resulution call to the container will create a new instance of the registered type, unless it is specified as a singleton during registration. To register a type as a singleton, just use the AsSingleInstance
method:
containerBuilder.RegisterType<MyClass>().As<IMyClass>().AsSingleInstance();
2. As self
If no alias is specified during the registration of a type, it will be registered as itself.
containerBuilder.RegisterType<MyClass>();
It can be resolved as:
var myInstance = container.Resolve<MyClass>();
If another alias is specified, but the registration as itself is also needed, the AsSelf
method must be used:
containerBuilder.RegisterType<MyClass>().As<IMyClass>().AsSelf();
3. Multiple registrations for the same alias
Multiple registrations can be made for the same alias:
containerBuilder.RegisterType<MyClassImplementation1>().As<IMyClass>();
containerBuilder.RegisterType<MyClassImplementation2>().As<IMyClass>();
In this case, the Resolve
method will return an instance of MyClassImplementation2
because it was the last one registered for that alias. By using the ResolveAll
method, all registrations for the alias will be returned:
IEnumerable<IMyClass> myInstances = container.ResolveAll<IMyClass>();
4. Factory method registrations
Factory methods can also be registered as Func<IContainer, T>
:
containerBuilder.RegisterFactoryMethod(container =>
{
var dep1 = container.Resolve<IDependency1>();
var dep2 = container.Resolve<IDependency2>();
return new MyClass(dep1, dep2);
}).As<IMyClass>();
These registrations can be resolved as their simple type counterparts:
var myInstance = container.Resolve<IMyClass>();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- 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.