AMillo.InjectableServices
2.0.0
dotnet add package AMillo.InjectableServices --version 2.0.0
NuGet\Install-Package AMillo.InjectableServices -Version 2.0.0
<PackageReference Include="AMillo.InjectableServices" Version="2.0.0" />
paket add AMillo.InjectableServices --version 2.0.0
#r "nuget: AMillo.InjectableServices, 2.0.0"
// Install AMillo.InjectableServices as a Cake Addin #addin nuget:?package=AMillo.InjectableServices&version=2.0.0 // Install AMillo.InjectableServices as a Cake Tool #tool nuget:?package=AMillo.InjectableServices&version=2.0.0
About The Project
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.
Getting Started
Installation
- .NET CLI
dotnet add package AMillo.InjectableServices --version 2.0.0
- Package Manager
Install-Package AMillo.InjectableServices -Version 2.0.0
Usage
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.
Service Lifetime
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.
Contributing
Visit the source repository for full documentation and contributing instructions:
Contact
Alejo Millo - alejo.millo@outlook.com
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.