Encamina.Enmarcha.DependencyInjection 8.1.5

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

// Install Encamina.Enmarcha.DependencyInjection as a Cake Tool
#tool nuget:?package=Encamina.Enmarcha.DependencyInjection&version=8.1.5

Dependency Injection

Nuget package

This project contains functionalities related to dependency injection. It primarily includes extension methods to simplify and enhance the capabilities of Microsoft.Extensions.DependencyInjection.

Setup

Nuget package

First, install NuGet. Then, install Encamina.Enmarcha.DependencyInjection from the package manager console:

PM> Install-Package Encamina.Enmarcha.DependencyInjection

.NET CLI:

First, install .NET CLI. Then, install Encamina.Enmarcha.DependencyInjection from the .NET CLI:

dotnet add package Encamina.Enmarcha.DependencyInjection

How to use

AutoRegisterServiceAttribute

You can use the AutoRegisterServiceAttribute attribute for your types, which enables automatic registration of themselves into the dependency injection container.

public interface IFoo
{
    void SayFoo();
}

[AutoRegisterService(ServiceLifetime.Singleton)]
public class ConsoleFoo : IFoo
{
    public void SayFoo()
    {
        Console.WriteLine("Foo");
    }
}

and a Program.cs or a similar entry point file in your project, add the following code:

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    // ...
});

// ... 

builder.Services.AddAutoRegisterServicesFromAssembly<Program>();

The above code adds types decorated with the AutoRegisterServiceAttribute (such as FooBar) into the service collection from the Program assembly.

With this, it can resolve the IFoo class with construction injection.

public class MyClass
{
    private readonly IFoo foo;

    public MyClass(IFoo foo)
    {
        this.foo = foo;
    }

    public void TestFoo()
    {
        foo.SayFoo();
    }
}

or Service Provider

var serviceProvider = services.BuildServiceProvider();
var foo = serviceProvider.GetRequiredService<IFoo>();

 foo.SayFoo();
Configuring ServiceLifetime

You can configure the ServiceLifetime to be Singleton, Scoped, or Transient.

public interface IFoo
{
    void SayFoo();
}

// Registers ConsoleFoo as Scoped
[AutoRegisterService(ServiceLifetime.Scoped)]
public class ConsoleFoo : IFoo
{
    public void SayFoo()
    {
        Console.WriteLine("Foo");
    }
}
Registering Alternative Types

You can specify the alternative types with which the class will be registered.

public interface IFoo
{
    void SayFoo();
}

public class ConsoleFooBar : IFoo
{
    public virtual void SayFoo()
    {
        Console.WriteLine("Foo");
    }
}

[AutoRegisterService(ServiceLifetime.Scoped, typeof(ConsoleFooBar), typeof(IFoo))]
public class RedConsoleFooBar : ConsoleFooBar
{
    public override void SayFoo()
    {
        Console.ForegroundColor = ConsoleColor.Red;
        base.SayFoo();
        Console.ResetColor();
    }
}

In the above code, it specifies that RedConsoleFooBar should register the alternative types ConsoleFooBar and IFoo. This means that when you resolve an instance of type RedConsoleFooBar, ConsoleFooBar, or IFoo, it will be resolved as RedConsoleFooBar.

// ...

var foo = builder.Services.BuildServiceProvider().GetRequiredService<IFoo>();
var consoleFooBar = builder.Services.BuildServiceProvider().GetRequiredService<ConsoleFooBar>();
var redConsoleFooBar = builder.Services.BuildServiceProvider().GetRequiredService<RedConsoleFooBar>();

bool fooIsRedConsoleFooBar = foo.GetType() == typeof(RedConsoleFooBar); // Is true
bool consoleFooBarIsRedConsoleFooBar = consoleFooBar.GetType() == typeof(RedConsoleFooBar); // Is true
bool fooRedConsoleFooBarIsRedConsoleFooBar = redConsoleFooBar.GetType() == typeof(RedConsoleFooBar); // Is true
Force only as implementation type

There are some scenarios when a class should be registered only as an implementation type, regardless the interfaces it implements. For this scenarios, sets ForceOnlyAsImplementationType to true.

public interface IFoo
{
    void SayFoo();
}

[AutoRegisterService(ServiceLifetime.Scoped, ForceOnlyAsImplementationType = true)]
public class ConsoleFooBar : IFoo
{
    public void SayFoo()
    {
        Console.WriteLine("Foo");
    }
}

Now, you can only resolve ConsoleFooBar directly using the class itself (ConsoleFooBar); attempting to resolve it through its interface will throw an exception.

// ...

// Throws System.InvalidOperationException: 'No service for type 'IFoo' has been registered.'
var foo = builder.Services.BuildServiceProvider().GetRequiredService<IFoo>();

// It's OK
var consoleFooBar = builder.Services.BuildServiceProvider().GetRequiredService<ConsoleFooBar>();
Not registering interfaces

Set RegisterInterfaces to false when the interfaces directly implemented by the class should not be registered as service types.

public interface IFoo
{
    void SayFoo();
}

[AutoRegisterService(ServiceLifetime.Scoped, RegisterInterfaces = false)]
public class ConsoleFooBar : IFoo
{
    public void SayFoo()
    {
        Console.WriteLine("Foo");
    }
}

Now, when attempting to resolve IFoo directly, an exception will be thrown.

// ...

// Throws System.InvalidOperationException: 'No service for type 'IFoo' has been registered.'
var foo = builder.Services.BuildServiceProvider().GetRequiredService<IFoo>();

// It's OK
var consoleFooBar = builder.Services.BuildServiceProvider().GetRequiredService<ConsoleFooBar>();
Registering inherited interfaces

Set IncludeInheritedInterfaces to true when the interfaces inherited by the class (i.e., implemented by base classes) should be registered as services types.

public interface IFoo
{
    void SayFoo();
}

public class ConsoleBaseFooBar : IFoo
{
    public virtual void SayFoo()
    {
    Console.WriteLine("Foo");
    }
}

[AutoRegisterService(ServiceLifetime.Scoped, IncludeInheritedInterfaces = true)]
public class RedConsoleFooBar : ConsoleBaseFooBar
{
    public override void SayFoo()
    {
        Console.ForegroundColor = ConsoleColor.Red;
        base.SayFoo();
        Console.ResetColor();
    }
}

Even though RedConsoleFooBar does not explicitly specify that it implements IFoo, since its base class ConsoleBaseFooBar implements it, you can resolve IFoo directly.

// ...

// It's OK
var foo = builder.Services.BuildServiceProvider().GetRequiredService<IFoo>();

Other functionalities

You can directly register a type or type/implementation by specifying the ServiceLifetime.

public interface IFoo
{
    void SayFoo();
}

public class ConsoleFooBar : IFoo
{
    public virtual void SayFoo()
    {
        Console.WriteLine("Foo");
    }
}

Next, in Program.cs or a similar entry point file in your project, add the following code.

// Register by type
builder.Services.AddType<ConsoleFooBar>(ServiceLifetime.Singleton);
builder.Services.TryAddType<ConsoleFooBar>(ServiceLifetime.Singleton);

// or TService, TImplementation
builder.Services.AddType<IFoo, ConsoleFooBar>(ServiceLifetime.Singleton);
builder.Services.TryAddType<IFoo, ConsoleFooBar>(ServiceLifetime.Singleton);

// or with implementation Instance
builder.Services.TryAddType<IFoo>(ServiceLifetime.Singleton, new ConsoleFooBar());

// or with factory method
builder.Services.TryAddType<IFoo, ConsoleFooBar>(ServiceLifetime.Singleton, provider => new ConsoleFooBar(provider.GetRequiredService<IDependency>()));

// ... the TryAddType and AddType methods can be used in various flavors to register services into an IServiceCollection.
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 (4)

Showing the top 4 NuGet packages that depend on Encamina.Enmarcha.DependencyInjection:

Package Downloads
Encamina.Enmarcha.Bot

Package Description

Encamina.Enmarcha.SemanticKernel.Connectors.Document

Package Description

Encamina.Enmarcha.SemanticKernel.Connectors.Memory

Package Description

Encamina.Enmarcha.Email.MailKit

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.1.6-preview-08 0 5/2/2024
8.1.6-preview-07 129 4/29/2024
8.1.6-preview-06 143 4/26/2024
8.1.6-preview-05 132 4/24/2024
8.1.6-preview-04 140 4/22/2024
8.1.6-preview-03 128 4/22/2024
8.1.6-preview-02 176 4/17/2024
8.1.6-preview-01 167 4/15/2024
8.1.5 157 4/15/2024
8.1.5-preview-15 129 4/10/2024
8.1.5-preview-14 155 3/20/2024
8.1.5-preview-13 120 3/18/2024
8.1.5-preview-12 140 3/13/2024
8.1.5-preview-11 139 3/13/2024
8.1.5-preview-10 128 3/13/2024
8.1.5-preview-09 139 3/12/2024
8.1.5-preview-08 107 3/12/2024
8.1.5-preview-07 121 3/8/2024
8.1.5-preview-06 291 3/8/2024
8.1.5-preview-05 107 3/7/2024
8.1.5-preview-04 129 3/7/2024
8.1.5-preview-03 112 3/7/2024
8.1.5-preview-02 176 2/28/2024
8.1.5-preview-01 194 2/19/2024
8.1.4 301 2/15/2024
8.1.3 167 2/13/2024
8.1.3-preview-07 97 2/13/2024
8.1.3-preview-06 133 2/12/2024
8.1.3-preview-05 108 2/9/2024
8.1.3-preview-04 111 2/8/2024
8.1.3-preview-03 124 2/7/2024
8.1.3-preview-02 116 2/2/2024
8.1.3-preview-01 103 2/2/2024
8.1.2 177 2/1/2024
8.1.2-preview-9 117 1/22/2024
8.1.2-preview-8 102 1/19/2024
8.1.2-preview-7 118 1/19/2024
8.1.2-preview-6 95 1/19/2024
8.1.2-preview-5 112 1/19/2024
8.1.2-preview-4 121 1/19/2024
8.1.2-preview-3 94 1/18/2024
8.1.2-preview-2 134 1/18/2024
8.1.2-preview-16 95 1/31/2024
8.1.2-preview-15 123 1/31/2024
8.1.2-preview-14 237 1/25/2024
8.1.2-preview-13 91 1/25/2024
8.1.2-preview-12 108 1/23/2024
8.1.2-preview-11 93 1/23/2024
8.1.2-preview-10 101 1/22/2024
8.1.2-preview-1 102 1/18/2024
8.1.1 162 1/18/2024
8.1.0 140 1/18/2024
8.0.3 217 12/29/2023
8.0.1 167 12/14/2023
8.0.0 189 12/7/2023
6.0.4.3 217 12/29/2023
6.0.4.2 233 12/20/2023
6.0.4.1 222 12/19/2023
6.0.4 195 12/4/2023
6.0.3.20 172 11/27/2023
6.0.3.19 160 11/22/2023