Are you tired of large Program.cs / Startup.cs files, with lots and lots of service dependency injections? Well...You are lucky!
InjectableServices is a simple feature that allows you to register your services without having to add them to the Program / Startup file, keeping them clean and smooth.
Here's why this is good:
- Cleaner and readable Program / Startup files, keep them small.
- Make your services ready-to-use just as you finish creating them, you don't even need to go into the Program / Startup file.
- .NET CLI
dotnet add package AMillo.InjectableServices --version 2.0.0
- Package Manager
Install-Package AMillo.InjectableServices -Version 2.0.0
-
Add the following using directive on your Program.cs / Startup.cs file
using AMillo.InjectableServices.Extensions.DependencyInjection;
-
Call the AddInjectableServices extension method using one of the following overloads
- AddInjectableServices()
//Look for injectable services inside all assemblies for current AppDomain builder.Services.AddInjectableServices();
- AddInjectableServicesFromAssemblies(Assembly[] assemblies)
//Look for injectable services inside the specified assemblies builder.Services.AddInjectableServicesFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());
- AddInjectableServicesFromAssembly(Assembly assembly)
//Look for injectable services inside a single specified assembly builder.Services.AddInjectableServicesFromAssembly(typeof(Program).Assembly);
- AddInjectableServices()
-
Mark your service interface with the [InjectableService] attribute.
using AMillo.InjectableServices.Attributes; [InjectableService] //Scoped by default internal interface IDemoService { string GetHelloWorld(); } internal sealed class DemoService : IDemoService { public string GetHelloWorld() { return "Hello World!"; } }
-
That's it! Your service will get registered automatically at startup.
By default, the [InjectableService] attribute will register your services as "Scoped".
But if you want, you can specify the lifetime for your service as follows:
- Mark your service interface with the [InjectableService] attribute passing the lifetime to the attribute's constructor.
using AMillo.InjectableServices.Attributes; //[InjectableService(Lifetime = ServiceLifetime.Singleton)] //For singleton //[InjectableService(Lifetime = ServiceLifetime.Transient)] //For transient [InjectableService(Lifetime = ServiceLifetime.Scoped)] //For scoped (default) internal interface IDemoService { string GetHelloWorld(); }
- That's it! Now your service will get register as Transient, Singleton or Scoped automatically on startup.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- 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
Alejo Millo - alejo.millo@outlook.com