Visium.Anima.DependencyInjection.SourceGeneration 1.0.0-alpha.6

This is a prerelease version of Visium.Anima.DependencyInjection.SourceGeneration.
There is a newer prerelease version of this package available.
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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Visium.Anima.DependencyInjection.SourceGeneration" Version="1.0.0-alpha.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Visium.Anima.DependencyInjection.SourceGeneration --version 1.0.0-alpha.6                
#r "nuget: Visium.Anima.DependencyInjection.SourceGeneration, 1.0.0-alpha.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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();
}
There are no supported framework assets in this package.

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 51 7/15/2024
1.0.0-alpha.10 69 4/12/2024
1.0.0-alpha.9 62 4/12/2024
1.0.0-alpha.8 55 4/10/2024
1.0.0-alpha.7 60 4/9/2024
1.0.0-alpha.6 63 4/9/2024
1.0.0-alpha.5 61 4/7/2024
1.0.0-alpha.4 60 4/7/2024
1.0.0-alpha.3 50 4/7/2024