A Simple, Lightweight & Useful Dependency Injector for .NET
Reduce boilerplate code and simplify dependency injection with attribute-based registration and property injection.
Automatically register services from namespaces without manual configuration.
Use simple attributes to control service registration and lifetime.
Inject dependencies directly into properties and fields with [InjectService].
Minimal overhead with maximum functionality. No heavy frameworks needed.
// Register all services from a namespace
services.AddServicesFrom("MyApp.Services");
// Or with custom configuration
services.AddServicesFrom("MyApp.Repositories", ServiceLifetime.Scoped, opts =>
{
opts.SelectInterface = interfaces => interfaces.FirstOrDefault(x => x.Name.StartsWith("I"));
opts.ImplementationBase = typeof(BaseRepository);
});
[RegisterAs(typeof(IBookService))]
[ServiceLifeTime(ServiceLifetime.Singleton)]
public class BookService : IBookService
{
// Your implementation
}
[DontRegister] // Exclude from automatic registration
public class InternalService { }
public class BookController : Controller
{
[InjectService]
public IBookService BookService { get; private set; }
[InjectService]
protected ILogger<BookController> logger;
// No constructor injection needed!
public IActionResult Index()
{
var books = BookService.GetAll();
logger.LogInformation("Retrieved {Count} books", books.Count);
return View(books);
}
}
// Startup.cs - Manual registration hell
services.AddScoped<IUserService, UserService>();
services.AddScoped<IProductService, ProductService>();
services.AddScoped<IOrderService, OrderService>();
services.AddScoped<IPaymentService, PaymentService>();
services.AddScoped<IShippingService, ShippingService>();
// ... 50 more lines
// Constructor injection clutter
public class OrderController : Controller
{
private readonly IUserService _userService;
private readonly IOrderService _orderService;
private readonly IPaymentService _paymentService;
public OrderController(
IUserService userService,
IOrderService orderService,
IPaymentService paymentService)
{
_userService = userService;
_orderService = orderService;
_paymentService = paymentService;
}
}
// Startup.cs - One line!
services.AddServicesFrom("MyApp.Services");
// Clean controllers with property injection
public class OrderController : Controller
{
[InjectService] public IUserService UserService { get; private set; }
[InjectService] public IOrderService OrderService { get; private set; }
[InjectService] public IPaymentService PaymentService { get; private set; }
// No constructor needed!
}
Join developers who are writing cleaner, more maintainable .NET applications.