Firefly.DependencyInjection 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Firefly.DependencyInjection --version 2.0.0                
NuGet\Install-Package Firefly.DependencyInjection -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="Firefly.DependencyInjection" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Firefly.DependencyInjection --version 2.0.0                
#r "nuget: Firefly.DependencyInjection, 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 Firefly.DependencyInjection as a Cake Addin
#addin nuget:?package=Firefly.DependencyInjection&version=2.0.0

// Install Firefly.DependencyInjection as a Cake Tool
#tool nuget:?package=Firefly.DependencyInjection&version=2.0.0                

Attribute-driven Service Registration

NuGet NuGet license


Register your services into the ServiceCollection with single attribute without writing kilometers of services.AddScoped(...) entries.

// Declaration
[RegisterScoped(Type = typeof(IMyService))]
public class MyServiceImplementation : IMyService
{}

// Consumer
[RegisterScoped]
public class MyServiceConsumer(){
    public MyServiceConsumer(IMyService myService)
    {
        ...
    }
}

Installation

Linux/OSX

dotnet add package Firefly.DependencyInjection

Windows

Install-Package Firefly.DependencyInjection

.csproj

<PackageReference Include="Firefly.DependencyInjection" />

Basic Setup

During application startup, find a IServiceCollection instance and call SetupFireflyServiceRegistration().

The location depends on your Hosting Model:

WebApplicationBuilder
var builder = WebApplication.CreateBuilder(args);
builder.Services.SetupFireflyServiceRegistration();
Older Startup.cs
public virtual void ConfigureServices(IServiceCollection services)
{
    services.SetupFireflyServiceRegistration();
}
Manually created ServiceCollction
var sc = new ServiceCollection()
sc.SetupFireflyServiceRegistration();

Usage

Service Lifetimes

// All three lifetimes are expressed by attributes

[RegisterTransient]
public class MyTransientService {}

[RegisterSingleton]
public class MyScopedService {}

[RegisterScoped]
public class MyScopedService {}

Using interfaces

// Declaration
[RegisterScoped(Type = typeof(IMyService))]
public class MyServiceImplementation : IMyService
{}

// Consumer
[RegisterScoped]
public class MyServiceConsumer(){
    public MyServiceConsumer(IMyService myService)
    {
        ...
    }
}

Multiple implementations of an interface

[RegisterScoped(Type = typeof(IMyService))] 
public class MyServiceA : IMyService {} // Variant A

[RegisterScoped(Type = typeof(IMyService))] 
public class MyServiceB : IMyService {} // Variant B

[RegisterScoped]
public class Consumer 
{
    // Services will be injected the same way as you're used to.
    public Consumer(ICollection<IMyService> myInstances){}
}

Picking single implementation of an interface from many

There can be a situation where you need to choose an implementation at the runtime. This is an example of choosing an filesystem provider based on a string during the application startup.

Let's have two different impl. of a IFileProvider interface.

// Implementation A
[RegisterScoped(Type = typeof(IFileProvider))]
public class BlobFileProvider : IFileProvider {} 

// Implementation B
[RegisterScoped(Type = typeof(IFileProvider))]
public class LocalFileProvider : IFileProvider {}

Application startup:

var useLocalFiles = true;

services.SetupFireflyServiceRegistration(builder => {
    if (useLocalFiles)
        builder.PickSingleImplementation<IFileProvider>(typeof(LocalFileProvider));
    else
        builder.PickSingleImplementation<IFileProvider>(typeof(BlobFileProvider));
});

The consuming service:

public class FilesystemConsumer {
    public FilesystemConsumer(IFileProvider provider){
        // provider will LocalFileProvider
    }
}

You may also use another two overrides that allow you to pass the Types via Type Parameters or via Type function argument.

// From DiRegistrationBuilder: 
public DiRegistrationBuilder PickSingleImplementation(Type interfaceType, Type concreteType);
public DiRegistrationBuilder PickSingleImplementation<TInterface>(Type concreteType);
public DiRegistrationBuilder PickSingleImplementation<TInterface, TConcrete>()

Registering services from other Assemblies

It's fully possible to include another assembly. All these assemblies will be scanned for [Register*] attributes.

⚠ Referencing an assembly is needed if you want to register services from another project in your solution.
services.SetupFireflyServiceRegistration(builder => {
    builder.UseAssembly("Example.Assembly.Name"); // Locate assembly by string
    builder.UseAssembly(Assembly.GetEntryAssembly()); // Specify assembly by the Assembly type and pass anything you need.
});
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Firefly.DependencyInjection:

Package Downloads
Firefly.net

REST API service template

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 374 10/5/2023
2.0.0 132 10/4/2023
1.1.0 1,130 1/21/2019
1.0.1 3,777 5/28/2017
1.0.0 1,041 5/28/2017