Siva.Extensions.DependencyInjection.Implementations 1.1.0

dotnet add package Siva.Extensions.DependencyInjection.Implementations --version 1.1.0
                    
NuGet\Install-Package Siva.Extensions.DependencyInjection.Implementations -Version 1.1.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="Siva.Extensions.DependencyInjection.Implementations" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Siva.Extensions.DependencyInjection.Implementations" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Siva.Extensions.DependencyInjection.Implementations" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Siva.Extensions.DependencyInjection.Implementations --version 1.1.0
                    
#r "nuget: Siva.Extensions.DependencyInjection.Implementations, 1.1.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.
#:package Siva.Extensions.DependencyInjection.Implementations@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Siva.Extensions.DependencyInjection.Implementations&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Siva.Extensions.DependencyInjection.Implementations&version=1.1.0
                    
Install as a Cake Tool

Siva.Extensions.DependencyInjection.Implementations

Extensions designed to add all implementations of an interface or class from a sequence of types or assemblies to the IServiceCollection. NuGet

How to use

Quick example:

using Microsoft.Extensions.DependencyInjection;
using Siva.Extensions.DependencyInjection;

...
public void ConfigureServices(IServiceCollection services)
{
    services.AddImplementations(typeof(IRepository<>), typeof(Program).Assembly);
}

This command will add all IRepository<T> implementations to the ServiceCollection from the specified assembly.

For example, here the UserRepository class will be added as an implementation type and IUserRepository as a service type:

public interface IRepository<T> 
{

}

public interface IUserRepository : IRepository<User>
{
    Task<User?> GetByNameAsync(string name);
}

public class UserRepository : IUserRepository
{
    public Task<User?> GetByNameAsync(string name)
    {
        ...
    }
}

And you can use this in your services as follows:

public class CurrentUserService
{
    private readonly IUserRepository userRepository;

    public CurrentUserService(IUserRepository userRepository) 
    {
        this.userRepository = userRepository;
    }
}

In the future, when we add another public service implementing this interface (for example, BookRepository and IBookRepository), we won't need to manually add it to the collection of services.

Need to add a class as a service type? No problem, you can do it like this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddImplementations(typeof(Factory<>), typeof(Program).Assembly);
}

...

public abstract class Factory<T>
{
    public abstract T Create();
}

public class ShoeFactory : Factory<Shoe>
{
    public override Shoe Create() 
    {
        return new Shoe();
    }
}

public class TrainFactory : Factory<Train>
{
    public override Train Create() 
    {
        return new Train();
    }
}

And here, the ShoeFactory and TrainFactory types will be added as implementations, and the Factory<Shoe> and Factory<Train> types will act as service types, respectively.

By default, the lifetime of the added services is ServiceLifetime.Scoped, but if necessary, you can set any other as follows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddImplementations(typeof(Factory<>), typeof(Program).Assembly, ServiceLifetime.Singleton);
}

For specific scenarios, you can also add service implementations directly from the type collection:

public void ConfigureServices(IServiceCollection services)
{
    services.AddImplementations(typeof(IRepository<>), typeof(Program).Assembly.GetTypes().Where(type => type.IsNotPublic));
}

In the example above, all non-public types that implement the IRepository<T> interface will be added.

Restrictions

You can't use a sealed type (struct or class with a sealed modifier) as a type to scan. So, this example will throw an ArgumentException:

public void ConfigureServices(IServiceCollection services)
{
    services.AddImplementations(typeof(int), typeof(Program).Assembly);
}

This behavior is used because sealed types can't be implemented or inherited, and therefore scanning for such types is a logical error.

See extra examples.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
1.1.0 128 5/2/2024
1.0.1 238 9/1/2023
1.0.0 168 8/31/2023