Easy to apply dependency injection into .NET Core Projects with Attributes like Spring Boot Applications
dotnet add package Nano.DependencyInjector
Define injection lifetime attribute to your injection class (Singleton, Scoped or Transient)
public interface ISingletonTestService
{
string Test();
}
[Singleton] // Add attribute to service class
public class SingletonTestService : ISingletonTestService
{
public SingletonTestService()
{
Console.WriteLine("Created singleton lifetime instance");
}
public string Test()
{
return "Singleton";
}
}
public interface IScopedTestService
{
string Test();
}
[Scoped] // Add attribute to service class
public class ScopedTestService : IScopedTestService
{
public ScopedTestService()
{
Console.WriteLine("Created scoped lifetime instance");
}
public string Test()
{
return "Scoped";
}
}
public interface ITransientTestService
{
string Test();
}
[Transient] // Add attribute to service class
public class TransientTestService : ITransientTestService
{
public TransientTestService()
{
Console.WriteLine("Created transient lifetime instance");
}
public string Test()
{
return "Transient";
}
}
Add code to ConfigureServices
on Startup.cs file and it will register all dependencies
services.RegisterDependencies(Assembly.GetExecutingAssembly());
- Setup CI for publishing package on nuget
- Add unit tests
- Register named typed dependency
- Inject name typed dependency
- Add property injection without constructor
- Register injectable multiple interfaces
- Generic interfaces
Feel the free for contribution. Open issues, PR and contact to me
This project is licensed under the MIT License