MEG.DependencyInjection 1.0.0

dotnet add package MEG.DependencyInjection --version 1.0.0
                    
NuGet\Install-Package MEG.DependencyInjection -Version 1.0.0
                    
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="MEG.DependencyInjection" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MEG.DependencyInjection" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MEG.DependencyInjection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MEG.DependencyInjection --version 1.0.0
                    
#r "nuget: MEG.DependencyInjection, 1.0.0"
                    
#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.
#:package MEG.DependencyInjection@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MEG.DependencyInjection&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MEG.DependencyInjection&version=1.0.0
                    
Install as a Cake Tool

MEG.DependencyInjection

This library provides automatic service registration and property injection capabilities for .NET applications. With MEG.DependencyInjection, you can register all your services and perform property injection with a single line of code.

Key features

<ul> <li> Automatic service registration based on marker interfaces </li> <li> Property injection to eliminate constructor injection hell</li> <li> Support for all service lifetimes (Scoped, Singleton, Transient)</li> <li> Keyed service registration support</li> <li> Flexible configuration options </li> </ul>

1- Implementation

Include MEG.DependencyInjection library in your project

dotnet add package MEG.DependencyInjection --version 1.0.0

Service registration

builder.Services.AddServices();

2- Service Registration

2.1- Basic Service Registration

Create a service interface that inherits from one of the marker interfaces:

// Scoped service example
public interface IUserService : IScopedService
{
    Task<User> GetUserAsync(int id);
    Task CreateUserAsync(User user);
}

public class UserService : IUserService
{
    public async Task<User> GetUserAsync(int id)
    {
        // Implementation here
        return new User();
    }

    public async Task CreateUserAsync(User user)
    {
        // Implementation here
    }
}

After calling <b> builder.Services.AddServices() </b> , UserService will be automatically registered as <b> scoped </b> service.

2.2- All Service Lifetimes

You can use all service lifetimes with marker interfaces: <ul> <li> IScopedService - Registers as Scoped </li> <li> ISingletonService - Registers as Singleton </li> <li> ITransientService - Registers as Transient </li> <li> IKeyedScopedService - Registers as Keyed Scoped </li> <li> IKeyedSingletonService - Registers as Keyed Singleton </li> <li> IKeyedTransientService - Registers as Keyed Transient </li> </ul>

2.3- Keyed Services

Example for keyed transient service:


public interface IPaymentService : IKeyedTransientService
{
    Task ProcessPaymentAsync(decimal amount);
}

public class CreditCardPaymentService : IPaymentService
{
    public static string ServiceKey => "CreditCard";

    public async Task ProcessPaymentAsync(decimal amount)
    {
        // Credit card payment implementation
    }
}

public class PayPalPaymentService : IPaymentService
{
    public static string ServiceKey => "PayPal";

    public async Task ProcessPaymentAsync(decimal amount)
    {
        // PayPal payment implementation
    }
}

// Usage
public class PaymentController : ControllerBase
{
    private readonly IServiceProvider _serviceProvider;

    public PaymentController(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    [HttpPost("pay")]
    public async Task<IActionResult> Pay([FromQuery] string paymentMethod, [FromBody] decimal amount)
    {
        var paymentService = _serviceProvider.GetRequiredKeyedService<IPaymentService>(paymentMethod);
        await paymentService.ProcessPaymentAsync(amount);
        return Ok();
    }
}

2.4- Configuration Options

You can configure assembly scanning and ignored types:


var option = new AddServiceOption
{
    Assembly = typeof(UserService).Assembly, // Scan specific assembly
    IgnoredTypes = new[] { typeof(UserService) } // Ignore specific types
};

builder.Services.AddServices(option);

3- Property Injection

3.1- Constructor Injection Hell

Without property injection, services with many dependencies become difficult to manage:

public class UserService : IUserService
{
private readonly IUserRepository _userRepository;
private readonly IEmailRepository _emailRepository;
private readonly ILogRepository _logRepository;
private readonly IFileRepository _fileRepository;
private readonly INotificationRepository _notificationRepository;
private readonly IAuditRepository _auditRepository;
private readonly IPermissionRepository _permissionRepository;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<UserService> _logger;
private readonly IHostEnvironment _hostEnvironment;

    public UserService(
        IUserRepository userRepository,
        IEmailRepository emailRepository,
        ILogRepository logRepository,
        IFileRepository fileRepository,
        INotificationRepository notificationRepository,
        IAuditRepository auditRepository,
        IPermissionRepository permissionRepository,
        IHttpContextAccessor httpContextAccessor,
        ILogger<UserService> logger,
        IHostEnvironment hostEnvironment)
    {
        _userRepository = userRepository;
        _emailRepository = emailRepository;
        _logRepository = logRepository;
        _fileRepository = fileRepository;
        _notificationRepository = notificationRepository;
        _auditRepository = auditRepository;
        _permissionRepository = permissionRepository;
        _httpContextAccessor = httpContextAccessor;
        _logger = logger;
        _hostEnvironment = hostEnvironment;
    }
}

3.2- Enable Property Injection

Enable property injection with configuration:

builder.Services.AddServices(option: new AddServiceOption
{
    IsPropertyInjectionActive = true
});

3.3- Property Injection Usage

With property injection, the same service becomes much cleaner:

public class UserService : IUserService
{
    public IUserRepository UserRepository { get; set; }
    public IEmailRepository EmailRepository { get; set; }
    public ILogRepository LogRepository { get; set; }
    public IFileRepository FileRepository { get; set; }
    public INotificationRepository NotificationRepository { get; set; }
    public IAuditRepository AuditRepository { get; set; }
    public IPermissionRepository PermissionRepository { get; set; }
    public IHttpContextAccessor HttpContextAccessor { get; set; }
    public ILogger<UserService> Logger { get; set; }
    public IHostEnvironment HostEnvironment { get; set; }
}

3.4- Base Service Only Injection

You can limit property injection to only IBaseService derived services:

builder.Services.AddServices(option: new AddServiceOption
{
    IsPropertyInjectionActive = true,
    IsOnlyBaseServiceInject = true
});

With this setting, only properties that inherit from IBaseService (like repositories) will be injected automatically.

3.5- Ignore Injection Attribute

You can exclude specific properties from injection using the <b>IgnoreInjectionAttribute</b>:

public class UserService : IUserService
{
    public IUserRepository UserRepository { get; set; }

    [IgnoreInjection]
    public ILogger<UserService> Logger { get; set; } // This will not be injected
    
    public UserService(ILogger<UserService> logger)
    {
        Logger = logger; // Manual injection through constructor
    }
}

4- Demonstration Project

You can access the demo project here

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 226 9/7/2025