PyNurseInjector

A Simple, Lightweight & Useful Dependency Injector for .NET

Reduce boilerplate code and simplify dependency injection with attribute-based registration and property injection.

Why PyNurseInjector?

Zero Configuration

Automatically register services from namespaces without manual configuration.

Attribute-Based

Use simple attributes to control service registration and lifetime.

Property Injection

Inject dependencies directly into properties and fields with [InjectService].

Lightweight

Minimal overhead with maximum functionality. No heavy frameworks needed.

Quick Example

// 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);
    }
}

Installation

Package Manager

Install-Package DotNurse.Injector
Install-Package DotNurse.Injector.AspNetCore

.NET CLI

dotnet add package DotNurse.Injector
dotnet add package DotNurse.Injector.AspNetCore

Before vs After

❌ Traditional Approach

// 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;
    }
}

✅ With PyNurseInjector

// 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!
}

Ready to Simplify Your Dependency Injection?

Join developers who are writing cleaner, more maintainable .NET applications.