Salt.Extensions.DependencyInjection 0.0.2

dotnet add package Salt.Extensions.DependencyInjection --version 0.0.2
NuGet\Install-Package Salt.Extensions.DependencyInjection -Version 0.0.2
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="Salt.Extensions.DependencyInjection" Version="0.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Salt.Extensions.DependencyInjection --version 0.0.2
#r "nuget: Salt.Extensions.DependencyInjection, 0.0.2"
#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 Salt.Extensions.DependencyInjection as a Cake Addin
#addin nuget:?package=Salt.Extensions.DependencyInjection&version=0.0.2

// Install Salt.Extensions.DependencyInjection as a Cake Tool
#tool nuget:?package=Salt.Extensions.DependencyInjection&version=0.0.2

Salt.Extensions.DependencyInjection

Welcome to Salt.Extensions.DependencyInjection, a "no bs" assembly scanner extension for Microsoft's DI container, just plug, register your assemblies into the service collection and play.

<sub>Note: If you want/need to have more granular experience, please check out Scrutor.</sub>

Installation

Install the Salt.Extensions.DependencyInjection NuGet package on all the projects that should benefit from the assembly scanning and service registration:

Package Manager Console:

Install-Package Salt.Extensions.DependencyInjection

Or dotnet CLI:

dotnet add package Salt.Extensions.DependencyInjection

In your executable project, make sure to add the Microsoft.Extensions.DependencyInjection package and create a service collection to register your assemblies.

How it works?

These are instructions for the simplest setup:

  1. Create a service implementation and decorate it with the Implementation attribute:

    using Microsoft.Extensions.DependencyInjection;
    
    // This attribute marks the class as a service implementation, it also controls the registration behavior for
    // interfaces, check the ImplementationAttribute class.
    [Implementation(ServiceLifetime.Singleton)]
    public class MyService
    {
        // Service Implementation
    }
    
  2. Register your assembly in the DI container:

    using Microsoft.Extensions.DependencyInjection;
    
    // ...
    var serviceCollection = ...;
    
    // This will scan the assembly where MyService is located and register all the types decorated with the
    // Implementation attribute.
    serviceCollection.AddAssembly<MyService>();
    
    // ...
    
  3. To use it like normal:

    using Microsoft.Extensions.DependencyInjection;
    // ...
    
    var serviceProvider = serviceCollection.BuildServiceProvider();
    var myService = serviceProvider.GetRequiredService<MyService>();
    // Use myService as you see fit.
    
    // ...
    

Why?

Well, I wanted a simple extension method, that's why this project is "no bs", meaning that there's no additional setup other than adding the assembly types to the service collection.

This implementation also moves all the information about the service definition to the service implementation itself, that way you don't have to open whatever file has all your Add{Lifetime}() methods.

And lastly, I started this project because it is a common practice for me to add some sort of automatic dependency injection to my projects, and keep startup files as small as they can be, you can think of it as a readability enhancement.

License

License is MIT, please check the LICENSE file for more information.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
0.0.2 250 2/7/2023
0.0.1 231 11/18/2022