Visium.Anima.DependencyInjection.SourceGeneration
1.0.0-alpha.6
See the version list below for details.
dotnet add package Visium.Anima.DependencyInjection.SourceGeneration --version 1.0.0-alpha.6
NuGet\Install-Package Visium.Anima.DependencyInjection.SourceGeneration -Version 1.0.0-alpha.6
<PackageReference Include="Visium.Anima.DependencyInjection.SourceGeneration" Version="1.0.0-alpha.6" />
paket add Visium.Anima.DependencyInjection.SourceGeneration --version 1.0.0-alpha.6
#r "nuget: Visium.Anima.DependencyInjection.SourceGeneration, 1.0.0-alpha.6"
// Install Visium.Anima.DependencyInjection.SourceGeneration as a Cake Addin #addin nuget:?package=Visium.Anima.DependencyInjection.SourceGeneration&version=1.0.0-alpha.6&prerelease // Install Visium.Anima.DependencyInjection.SourceGeneration as a Cake Tool #tool nuget:?package=Visium.Anima.DependencyInjection.SourceGeneration&version=1.0.0-alpha.6&prerelease
Anima.DependencyInjection.SourceGeneration
Source generators to reduce boilerplate code when working with Microsoft.Extensions.DependencyInjection.
RegisterService
This feature eases the burden of manually adding services to the dependency injection container every time a new service class is created. A constructor is also automatically generated for each service class that accepts variables corresponding to each unassigned readonly field in said class.
Usage
Set any service classes as partial
and mark them with the [RegisterService]
attribute to add them
to an automatically generated extension method for the current assembly.
OnConstruct
If you need to run additional code inside the automatically generated constructor, you can simply mark
a method in the class with [OnConstruct]
and it will run at the end of the constructor.
Full Example
Given the following classes in a project named MyClassLibrary:
[RegisterService(ServiceLifetime.Singleton)]
public partial class ServiceCounterService
{
public int ServiceCount { get; set; }
}
public interface IRepository<TEntity>;
[RegisterService(ServiceLifetime.Transient)]
public partial class UserRepository : IRepository<User>
{
private readonly ServiceCounterService _serviceCounter;
[OnConstruct]
private void OnConstruct()
{
_serviceCounter.ServiceCount++;
}
}
This output is generated:
public static class MyClassLibraryServiceExtensions
{
public static IServiceCollection AddMyClassLibrary(this IServiceCollection services)
{
services.AddSingleton<ServiceCounterService>();
services.AddTransient<IRepository<User>, UserRepository>();
}
}
public partial class UserRepository
{
public UserRepository(ServiceCounterService serviceCounter)
{
_serviceCounter = serviceCounter;
OnConstruct();
}
}
Then you simply call the extension method from the project that needs to consume your services:
public ServiceProvider ConfigureServices()
{
return new ServiceCollection()
.AddMyClassLibrary()
.BuildServiceProvider();
}
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 |
---|---|---|
1.0.0-alpha.11 | 49 | 7/15/2024 |
1.0.0-alpha.10 | 67 | 4/12/2024 |
1.0.0-alpha.9 | 60 | 4/12/2024 |
1.0.0-alpha.8 | 53 | 4/10/2024 |
1.0.0-alpha.7 | 58 | 4/9/2024 |
1.0.0-alpha.6 | 61 | 4/9/2024 |
1.0.0-alpha.5 | 59 | 4/7/2024 |
1.0.0-alpha.4 | 58 | 4/7/2024 |
1.0.0-alpha.3 | 48 | 4/7/2024 |