DependencyInjection.SourceGenerator.LightInject
2.0.0
dotnet add package DependencyInjection.SourceGenerator.LightInject --version 2.0.0
NuGet\Install-Package DependencyInjection.SourceGenerator.LightInject -Version 2.0.0
<PackageReference Include="DependencyInjection.SourceGenerator.LightInject" Version="2.0.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add DependencyInjection.SourceGenerator.LightInject --version 2.0.0
#r "nuget: DependencyInjection.SourceGenerator.LightInject, 2.0.0"
// Install DependencyInjection.SourceGenerator.LightInject as a Cake Addin #addin nuget:?package=DependencyInjection.SourceGenerator.LightInject&version=2.0.0 // Install DependencyInjection.SourceGenerator.LightInject as a Cake Tool #tool nuget:?package=DependencyInjection.SourceGenerator.LightInject&version=2.0.0
DependencyInjection.SourceGenerator.LightInject
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.
var services = new ServiceCollection();
services.AddMyProject();
### LightInject
```csharp
[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
}
}
Register all services in the project
You can also register all services in an project by adding the RegisterAll attribute to the assembly. This will register all implementations of the specified type.
using DependencyInjection.SourceGenerator.Contracts.Attributes;
[assembly: RegisterAll<IExampleService>]
namespace RootNamespace.Services;
public interface IExampleService
{
string GetExample();
}
public class ExampleService1 : IExampleService
{
public string GetExample()
{
return "Example 1";
}
}
public class ExampleService2 : IExampleService
{
public string GetExample()
{
return "Example 2";
}
}
this will generate the following code:
public class CompositionRoot : ICompositionRoot
{
public static void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.Register<IExampleService, ExampleService1>(new PerContainerLifetime());
serviceRegistry.Register<IExampleService, ExampleService2>(new PerContainerLifetime());
}
}
Lifetime
The lifetime is an enum with the following values:
- Transient
- Scoped
- Singleton
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.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.
Version | Downloads | Last updated |
---|---|---|
2.0.0 | 114 | 8/30/2024 |
1.6.0 | 199 | 2/14/2024 |
1.5.1 | 195 | 1/15/2024 |
1.5.0 | 134 | 1/15/2024 |
1.4.3 | 154 | 1/10/2024 |
1.4.2 | 123 | 1/10/2024 |
1.4.1 | 136 | 1/9/2024 |
1.4.0 | 135 | 1/9/2024 |
1.3.3 | 152 | 1/8/2024 |
1.3.2 | 120 | 1/8/2024 |
1.3.1 | 120 | 1/8/2024 |
1.3.0 | 134 | 1/8/2024 |
1.2.2 | 153 | 1/8/2024 |
1.2.2-alpha3 | 88 | 1/8/2024 |
1.2.2-alpha2 | 126 | 1/8/2024 |
1.2.1 | 116 | 1/5/2024 |
1.2.1-alpha6 | 165 | 1/5/2024 |
1.2.1-alpha5 | 93 | 1/5/2024 |
1.2.1-alpha2 | 95 | 1/5/2024 |
1.2.1-alpha1 | 131 | 1/5/2024 |
1.2.0 | 115 | 1/5/2024 |
1.1.2 | 133 | 1/5/2024 |
1.1.1 | 100 | 1/5/2024 |
1.1.0 | 158 | 1/5/2024 |
1.0.2 | 143 | 1/5/2024 |
1.0.1 | 130 | 1/4/2024 |
1.0.0 | 140 | 1/4/2024 |