Futurum.Microsoft.Extensions.DependencyInjection 1.0.6

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

// Install Futurum.Microsoft.Extensions.DependencyInjection as a Cake Tool
#tool nuget:?package=Futurum.Microsoft.Extensions.DependencyInjection&version=1.0.6                

Futurum.Microsoft.Extensions.DependencyInjection

license CI Coverage Status NuGet version

A dotnet library that extends Microsoft.Extensions.DependencyInjection by adding support for modules, startables and attribute based registration.

  • Autodiscovery of DependencyInjection registrations, based on attributes and Source Generators
  • Autodiscovery of DependencyInjection modules, based on attributes and Source Generators
  • Autodiscovery of DependencyInjection startables, based on attributes and Source Generators
  • Roslyn Analysers to help build your WebApiEndpoint(s), using best practices
  • Integration with Futurum.Core]

TryGetService

Try to get the service object of the specified type.

var result = serviceProvider.TryGetService<ITestService>();

Modules

A module allows you to break up registration into logical units.

Module can either be registered using IModule interface and AddModule extension method, or by using the RegisterAsDependencyInjectionModule attribute.

IModule interface and AddModule extension method

IModule interface

Implements this interface to create a module.

public class TestModule : IModule
{
    public void Load(IServiceCollection services)
    {
        services.AddSingleton<ITestService, TestService>();
    }
}
AddModule extension method

Allows you to register a module.

services.AddModule<TestModule>();
services.AddModule(new TestModule());

Attribute based module

You can also register modules using attributes.

  • RegisterAsDependencyInjectionModule attribute
public class Module
{
    [RegisterAsDependencyInjectionModule]
    public void Load(IServiceCollection services)
    {
    }
}
public static class Module
{
    [RegisterAsDependencyInjectionModule]
    public static void Load(IServiceCollection services)
    {
    }
}

Startables

A startable is resolved at the start of the application lifecycle and is a place to perform actions as soon as the DependencyInjection container is built.

Startable can either be registered using IStartable interface and AddStartable extension method, or by using the RegisterAsDependencyInjectionStartable attribute.

IStartable interface and AddStartable extension method

IStartable interface

Implements this interface to create a startable.

public class TestStartable : IStartable
{
    public Task Start()
    {
        // Do something
    }
}
AddStartable extension method

Allows you to register a startable.

services.AddStartable<TestStartable>();
services.AddStartable(new TestStartable());

Attribute based startable

You can also register modules using attributes.

  • RegisterAsDependencyInjectionStartable attribute
public class Startable
{
    [RegisterAsDependencyInjectionStartable]
    public Task Start()
    {
        // Do something
    }
}
public static class Startable
{
    [RegisterAsDependencyInjectionStartable]
    public static Task Start()
    {
        // Do something
    }
}

BuildServiceProviderWithStartables extension method

If you are manually building the IServiceProvider, then you need to use BuildServiceProviderWithStartablesAsync extension method. This will build the container as usual, but also starts all IStartable instances.

var serviceProvider = await services.BuildServiceProviderWithStartablesAsync();

Attribute based registration

You can also register services using attributes.

There are generic attributes available allowing you to specify the ServiceType, if your service implements multiple interfaces.

RegisterAsSingleton attribute

[RegisterAsSingleton]
public class Service : IService
{
}
[RegisterAsSingleton<IService2>]
public class Service : IService1, IService2
{
}

RegisterAsScoped attribute

[RegisterAsScoped]
public class Service : IService
{
}
[RegisterAsScoped<IService2>]
public class Service : IService1, IService2
{
}

RegisterAsTransient attribute

[RegisterAsTransient]
public class Service : IService
{
}
[RegisterAsTransient<IService2>]
public class Service : IService1, IService2
{
}

DuplicateRegistrationStrategy

  • Try - Adds the new registration, if the service hasn't already been registered
  • Replace - Removes any existing registration and then adds the new registration
  • Add - Adds the new registration, irrespective of if its previously been registered

NOTE - This defaults to Try

[RegisterAsSingleton(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Add)]
public class Service : IService
{
}

InterfaceRegistrationStrategy

  • Self - Registers the service as itself
  • ImplementedInterfaces - Registers the service as each its implemented interfaces
  • SelfWithInterfaces - Registers the service as itself and each its implemented interfaces

NOTE - This defaults to SelfWithInterfaces

[RegisterAsSingleton(InterfaceRegistrationStrategy = InterfaceRegistrationStrategy.ImplementedInterfaces)]
public class Service : IService
{
}

Roslyn Analysers

  • FMEDI0001 - Invalid Module Parameter
  • FMEDI0002 - Missing Module Parameter
  • FMEDI0003 - Non empty constructor found on Module
  • FMEDI0004 - Non empty constructor found on Startable
  • FMEDI0005 - Non async method found on Startable
  • FMEDI0006 - Non void method found on Module
  • FMEDI0007 - Register ServiceType not implemented by class
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 (5)

Showing the top 5 NuGet packages that depend on Futurum.Microsoft.Extensions.DependencyInjection:

Package Downloads
Futurum.WebApiEndpoint

A dotnet library that allows you to build WebApiEndpoints using a vertical slice architecture approach. Provides a structured way to create REST apis in dotnet without controllers.

Futurum.ApiEndpoint

A dotnet library, that is the base for ApiEndpoints in Futurum.

Futurum.WebApiEndpoint.Micro

A dotnet library that allows you to build WebApiEndpoints using a vertical slice architecture approach. Built on dotnet 8 and minimal apis.

Futurum.Core.Polly

Small dotnet library, allowing you to use [Polly](https://github.com/App-vNext/Polly) with Futurum.Core, based on the concepts behind 'Railway Oriented Programming'.

Futurum.WebApiEndpoint.Micro.Core.Extensions

A dotnet library that extends Futurum.WebApiEndpoint.Micro, to make it fully compatible with Futurum.Core.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 249 12/24/2023
2.0.0 340 12/5/2023
1.0.11 237 4/21/2023
1.0.10 198 4/20/2023
1.0.9 193 4/19/2023
1.0.8 194 4/19/2023
1.0.7 213 4/17/2023
1.0.6 188 4/17/2023
1.0.5 192 4/16/2023
1.0.4 214 4/7/2023
1.0.3 1,232 6/3/2022
1.0.2 2,312 2/6/2022
1.0.1 655 2/5/2022
1.0.0 931 1/28/2022