Skip to content

Fast & easy injectable services feature to keep Program.cs / Startup.cs clean.

License

Notifications You must be signed in to change notification settings

AlejoMillo00/AMillo.InjectableServices

Repository files navigation

Contributors Forks Stargazers Issues LinkedIn


InjectableServices

Report Bug · Request Feature

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

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Contact

Alejo Millo - alejo.millo@outlook.com

About

Fast & easy injectable services feature to keep Program.cs / Startup.cs clean.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages