DependencyInjection.SourceGenerator
0.1.1
DependencyInjection.SourceGenerator.LightInject
Additional DetailsThis package has been renamed to DependencyInjection.SourceGenerator.LightInject in order to make it more obvious which dependency injection library it generates code for. There is also a new DependencyInjection.SourceGenerator.Microsoft package that supports the microsoft dependency injection library
dotnet add package DependencyInjection.SourceGenerator --version 0.1.1
NuGet\Install-Package DependencyInjection.SourceGenerator -Version 0.1.1
<PackageReference Include="DependencyInjection.SourceGenerator" Version="0.1.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add DependencyInjection.SourceGenerator --version 0.1.1
#r "nuget: DependencyInjection.SourceGenerator, 0.1.1"
// Install DependencyInjection.SourceGenerator as a Cake Addin #addin nuget:?package=DependencyInjection.SourceGenerator&version=0.1.1 // Install DependencyInjection.SourceGenerator as a Cake Tool #tool nuget:?package=DependencyInjection.SourceGenerator&version=0.1.1
DependencyInjection.SourceGenerator
Register services using attributes instead of registering in code.
Usage
Add the "Register" attribute to the class you want to register. The attribute takes a type and a lifetime. The type is the type you want to register and the lifetime is the lifetime of the service. The lifetime is optional and defaults to Transient.
[Register(ServiceName = "ServiceName", Lifetime = Lifetime.Singleton)]
public class ExampleService : IExampleService
{
public string GetExample()
{
return "Example";
}
}
public interface IExampleService
{
string GetExample();
}
Generates a class CompositionRoot
public class CompositionRoot : ICompositionRoot
{
public static void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.Register<IExampleService, ExampleService>("ServiceName", new PerContainerLifetime());
}
}
If you already have a class CompositionRoot defined, the generated class will be made partial. Remeber to make your CompositionRoot partial as well. It will also call a method RegisterServices on the existing CompositionRoot class (this must be defined).
public partial class CompositionRoot : ICompositionRoot
{
public static void Compose(IServiceRegistry serviceRegistry)
{
RegisterServices(serviceRegistry);
serviceRegistry.Register<IExampleService, ExampleService>("ServiceName", new PerContainerLifetime());
}
}
The final existing CompositionRoot class must look like this:
public partial class CompositionRoot
{
public void RegisterServices(IServiceRegistry serviceRegistry)
{
// Register services here
}
}
Lifetime
The lifetime is an enum with the following values:
- Transient
- Scoped
- Singleton
Misc
Current only works with LightInject.
Learn more about Target Frameworks and .NET Standard.
This package has 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.
Version | Downloads | Last updated | |
---|---|---|---|
0.1.1 | 421 | 12/14/2022 | |
0.1.0 | 347 | 12/14/2022 | |
0.0.3 | 332 | 12/14/2022 | |
0.0.3-alpha4 | 283 | 12/14/2022 | |
0.0.3-alpha3 | 272 | 12/14/2022 | |
0.0.3-alpha2 | 239 | 12/14/2022 | |
0.0.3-alpha1 | 281 | 12/14/2022 | |
0.0.2 | 354 | 12/14/2022 |