AMillo.InjectableServices 2.0.0

dotnet add package AMillo.InjectableServices --version 2.0.0                
NuGet\Install-Package AMillo.InjectableServices -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AMillo.InjectableServices" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AMillo.InjectableServices --version 2.0.0                
#r "nuget: AMillo.InjectableServices, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

  1. Add the following using directive on your Program.cs / Startup.cs file

    using AMillo.InjectableServices.Extensions.DependencyInjection;
    
  2. 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); 
      
  3. 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!";
         }
     }
    
  4. 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:

  1. 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();
     }
    
  2. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 115 5/2/2024
1.0.0 116 4/28/2024