The SuperInject library provides a convenient way to automatically register services and repositories in the Microsoft.Extensions.DependencyInjection container using custom attributes. It simplifies the dependency registration process, allowing developers to annotate their classes with attributes and then automatically register those classes with the dependency injection container.
To use the SuperInject library in your project, install the NuGet package:
dotnet add package SuperInject
In your project, use the SuperInject library to automatically register services and repositories by marking your classes with the appropriate attributes.
The ServiceAttribute
is used to mark classes as services and define their injection type.
ServiceAttribute(ServiceLifetime serviceLifetime)
: Creates a new instance of theServiceAttribute
with the specifiedServiceLifetime
.
using SuperInject;
// Mark your class with the ServiceAttribute
[Service(ServiceLifetime.Singleton)] // Choose the appropriate ServiceLifetime
public class MyService : IMyService
{
public void DoSomething()
{
// Implementation details...
}
}
The RepositoryAttribute
is used to mark classes as repositories and define their injection type.
RepositoryAttribute(ServiceLifetime serviceLifetime)
: Creates a new instance of theRepositoryAttribute
with the specifiedServiceLifetime
.
using SuperInject;
// Mark your class with the RepositoryAttribute
[Repository(ServiceLifetime.Scoped)] // Choose the appropriate ServiceLifetime
public class MyRepository : IMyRepository
{
public void DoSomething()
{
// Implementation details...
}
}
The AddSuperInject extension method simplifies the process of automatically registering services and repositories in the dependency injection container.
public static void AddSuperInject(this IServiceCollection services);
This method scans the assemblies in the current domain for classes marked with ServiceAttribute
or RepositoryAttribute
. It then registers these classes in the dependency injection container based on their specified injection type (ServiceLifetime
).
// ConfigureServices method in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Use AddSuperInject method to automatically register services and repositories
services.AddSuperInject();
// Your other service registrations go here...
}
While SuperInject simplifies the dependency injection process, it's essential to be mindful of circular dependencies. Circular dependencies can lead to infinite recursion, and though the library attempts to handle them gracefully, it's advisable to design your application to avoid such scenarios.
The SuperInject NuGet Library provides an elegant solution for managing dependencies in .NET applications. By leveraging attributes and extension methods, developers can effortlessly register services and repositories, enhancing code readability and maintainability. Install SuperInject in your project today to experience the benefits of streamlined dependency injection.
SuperInject is released under the MIT License, offering flexibility for customization and extension to suit your specific requirements.