JZen.AutoInject
1.0.0
See the version list below for details.
dotnet add package JZen.AutoInject --version 1.0.0
NuGet\Install-Package JZen.AutoInject -Version 1.0.0
<PackageReference Include="JZen.AutoInject" Version="1.0.0" />
<PackageVersion Include="JZen.AutoInject" Version="1.0.0" />
<PackageReference Include="JZen.AutoInject" />
paket add JZen.AutoInject --version 1.0.0
#r "nuget: JZen.AutoInject, 1.0.0"
#:package JZen.AutoInject@1.0.0
#addin nuget:?package=JZen.AutoInject&version=1.0.0
#tool nuget:?package=JZen.AutoInject&version=1.0.0
π AutoInject
Stop writing constructor injection boilerplate! π
AutoInject is a lightweight library that brings attribute-based dependency injection to .NET, eliminating the need for constructor injection hell.
π― Why AutoInject?
Tired of this? π€
public class OrderService
{
private readonly IRepository _repository;
private readonly IEmailService _emailService;
private readonly ILogger<OrderService> _logger;
private readonly IPaymentService _paymentService;
private readonly INotificationService _notificationService;
public OrderService(
IRepository repository,
IEmailService emailService,
ILogger<OrderService> logger,
IPaymentService paymentService,
INotificationService notificationService)
{
_repository = repository;
_emailService = emailService;
_logger = logger;
_paymentService = paymentService;
_notificationService = notificationService;
}
}
With AutoInject, write this instead: β¨
[AutoInject]
public class OrderService
{
[Injectable] private readonly IRepository _repository;
[Injectable] private readonly IEmailService _emailService;
[Injectable] private readonly ILogger<OrderService> _logger;
[Injectable] private readonly IPaymentService _paymentService;
[Injectable] private readonly INotificationService _notificationService;
// Clean constructor - no dependencies!
public OrderService() { }
}
π Key Features
- β Zero Boilerplate: No more constructor injection bloat
- β
Attribute-Based: Simple
[AutoInject]
and[Injectable]
attributes - β ASP.NET Core Integration: Seamless integration with built-in DI container
- β Automatic Registration: Auto-scan and register your classes
- β Scoped Management: Automatic scope management for web requests
- β Logger Support: Built-in logger injection support
- β Performance Optimized: Type caching for reflection operations
- β Thread-Safe: Safe for concurrent operations
π¦ Installation
# Coming soon to NuGet
Install-Package JZen.AutoInject
Or add the project reference:
<ProjectReference Include="path/to/AutoInject/AutoInject.csproj" />
π Quick Start
1. Configure your services in Program.cs
using AutoInject.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Register your services
builder.Services.AddScoped<IRepository, Repository>();
builder.Services.AddScoped<IEmailService, EmailService>();
// π₯ Auto-register all classes marked with [AutoInject]
builder.Services.AddAutoInjectClasses(Assembly.GetExecutingAssembly());
var app = builder.Build();
// π₯ Enable AutoInject
app.UseFactoryDependencyInjection();
app.Run();
2. Mark your classes with [AutoInject]
using AutoInject.Attributes;
[AutoInject]
public class OrderService
{
[Injectable] private readonly IRepository _repository;
[Injectable] private readonly IEmailService _emailService;
[Injectable] private readonly ILogger<OrderService> _logger;
public async Task ProcessOrderAsync(Order order)
{
_logger.LogInformation("Processing order {OrderId}", order.Id);
await _repository.SaveAsync(order);
await _emailService.SendConfirmationAsync(order.CustomerEmail);
_logger.LogInformation("Order {OrderId} processed successfully", order.Id);
}
}
3. Use your services normally
[ApiController]
[Route("api/[controller]")]
public class OrdersController : ControllerBase
{
private readonly OrderService _orderService;
public OrdersController(OrderService orderService)
{
_orderService = orderService;
}
[HttpPost]
public async Task<IActionResult> CreateOrder([FromBody] Order order)
{
await _orderService.ProcessOrderAsync(order);
return Ok();
}
}
π Advanced Usage
Inherit from InjectBase
(Alternative Approach)
using AutoInject;
public class OrderService : InjectBase
{
[Injectable] private readonly IRepository _repository;
[Injectable] private readonly IEmailService _emailService;
// Logger is automatically available as _logger
public void ProcessOrder()
{
_logger.LogInformation("Processing order...");
// Your logic here
}
}
Automatic Interface Registration
// Register all interfaces from a specific namespace
builder.Services.AddScopedInjection("MyApp.Application.UseCases");
Multiple Assembly Registration
var assemblies = new[]
{
Assembly.GetExecutingAssembly(),
typeof(SomeOtherClass).Assembly
};
builder.Services.AddAutoInjectClasses(assemblies);
Custom Service Lifetimes
// Register as Singleton
builder.Services.AddAutoInjectClasses(
Assembly.GetExecutingAssembly(),
ServiceLifetime.Singleton
);
π§ How It Works
AutoInject uses a combination of:
- Reflection: To discover types and properties marked with attributes
- Custom Factory: Creates instances and injects dependencies
- Interceptors: Automatically process classes during instantiation
- Middleware: Manages scoped services lifecycle in web applications
- Caching: Optimizes reflection operations for better performance
Architecture Overview
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β [AutoInject] βββββΆβ Factory βββββΆβ DI Container β
β Classes β β + Interceptor β β (ASP.NET) β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β [Injectable] β β Auto-Factory β β Scope Mgmt β
β Properties β β Registration β β Middleware β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
π― Best Practices
β DO
[AutoInject]
public class UserService
{
[Injectable] private readonly IUserRepository _userRepository;
[Injectable] private readonly ILogger<UserService> _logger;
// Use private readonly fields
// Mark with [Injectable]
// Keep constructors clean
}
β DON'T
[AutoInject]
public class UserService
{
[Injectable] public IUserRepository UserRepository; // Don't use public fields
[Injectable] private ILogger<UserService> _logger; // Don't use mutable fields
// Don't mix constructor injection with AutoInject
public UserService(ISomeService service) { }
}
π Comparison
Feature | Constructor Injection | AutoInject |
---|---|---|
Boilerplate Code | High π | Minimal β¨ |
Readability | Cluttered π΅ | Clean π§Ή |
Refactoring | Painful π€ | Easy π― |
Performance | Faster πββοΈ | Slightly Slower πΆββοΈ |
Learning Curve | Standard π | Minimal π |
π€ Contributing
We welcome contributions! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
π License
This project is licensed under the MIT License - see the LICENSE file for details.
πββοΈ Support
- π§ Email: [jaelsonrc@hotmail.com]
- π Issues: GitHub Issues
- π¬ Discussions: GitHub Discussions
Made with β€οΈ by developers who hate constructor injection boilerplate!
"Life's too short for constructor injection hell" - Anonymous Developer
Product | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release of JZen.AutoInject library. Features include attribute-based dependency injection, automatic service registration, and seamless ASP.NET Core integration.