Encamina.Enmarcha.DependencyInjection 8.1.6-preview-06

This is a prerelease version of Encamina.Enmarcha.DependencyInjection.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Encamina.Enmarcha.DependencyInjection --version 8.1.6-preview-06                
NuGet\Install-Package Encamina.Enmarcha.DependencyInjection -Version 8.1.6-preview-06                
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.6-preview-06" />                
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.6-preview-06                
#r "nuget: Encamina.Enmarcha.DependencyInjection, 8.1.6-preview-06"                
#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.6-preview-06&prerelease

// Install Encamina.Enmarcha.DependencyInjection as a Cake Tool
#tool nuget:?package=Encamina.Enmarcha.DependencyInjection&version=8.1.6-preview-06&prerelease                

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 (5)

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

Package Downloads
Encamina.Enmarcha.Bot

Package Description

Encamina.Enmarcha.SemanticKernel.Connectors.Memory

Package Description

Encamina.Enmarcha.Email.MailKit

Package Description

Encamina.Enmarcha.SemanticKernel.Connectors.Document

Package Description

Encamina.Enmarcha.Conversation

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.2.0 577 10/22/2024
8.2.0-preview-01-m01 202 9/17/2024
8.1.9-preview-03 202 11/19/2024
8.1.9-preview-02 168 10/22/2024
8.1.9-preview-01 392 10/4/2024
8.1.8 370 9/23/2024
8.1.8-preview-07 730 9/12/2024
8.1.8-preview-06 341 9/11/2024
8.1.8-preview-05 216 9/10/2024
8.1.8-preview-04 458 8/16/2024
8.1.8-preview-03 275 8/13/2024
8.1.8-preview-02 205 8/13/2024
8.1.8-preview-01 215 8/12/2024
8.1.7 232 8/7/2024
8.1.7-preview-09 240 7/3/2024
8.1.7-preview-08 201 7/2/2024
8.1.7-preview-07 162 6/10/2024
8.1.7-preview-06 172 6/10/2024
8.1.7-preview-05 196 6/6/2024
8.1.7-preview-04 179 6/6/2024
8.1.7-preview-03 201 5/24/2024
8.1.7-preview-02 200 5/10/2024
8.1.7-preview-01 240 5/8/2024
8.1.6 1,480 5/7/2024
8.1.6-preview-08 172 5/2/2024
8.1.6-preview-07 193 4/29/2024
8.1.6-preview-06 494 4/26/2024
8.1.6-preview-05 220 4/24/2024
8.1.6-preview-04 223 4/22/2024
8.1.6-preview-03 187 4/22/2024
8.1.6-preview-02 251 4/17/2024
8.1.6-preview-01 288 4/15/2024
8.1.5 244 4/15/2024
8.1.5-preview-15 206 4/10/2024
8.1.5-preview-14 239 3/20/2024
8.1.5-preview-13 193 3/18/2024
8.1.5-preview-12 209 3/13/2024
8.1.5-preview-11 199 3/13/2024
8.1.5-preview-10 228 3/13/2024
8.1.5-preview-09 204 3/12/2024
8.1.5-preview-08 184 3/12/2024
8.1.5-preview-07 200 3/8/2024
8.1.5-preview-06 356 3/8/2024
8.1.5-preview-05 195 3/7/2024
8.1.5-preview-04 219 3/7/2024
8.1.5-preview-03 212 3/7/2024
8.1.5-preview-02 285 2/28/2024
8.1.5-preview-01 235 2/19/2024
8.1.4 392 2/15/2024
8.1.3 229 2/13/2024
8.1.3-preview-07 173 2/13/2024
8.1.3-preview-06 221 2/12/2024
8.1.3-preview-05 202 2/9/2024
8.1.3-preview-04 198 2/8/2024
8.1.3-preview-03 198 2/7/2024
8.1.3-preview-02 220 2/2/2024
8.1.3-preview-01 169 2/2/2024
8.1.2 258 2/1/2024
8.1.2-preview-9 216 1/22/2024
8.1.2-preview-8 186 1/19/2024
8.1.2-preview-7 177 1/19/2024
8.1.2-preview-6 160 1/19/2024
8.1.2-preview-5 190 1/19/2024
8.1.2-preview-4 173 1/19/2024
8.1.2-preview-3 163 1/18/2024
8.1.2-preview-2 192 1/18/2024
8.1.2-preview-16 203 1/31/2024
8.1.2-preview-15 201 1/31/2024
8.1.2-preview-14 352 1/25/2024
8.1.2-preview-13 175 1/25/2024
8.1.2-preview-12 190 1/23/2024
8.1.2-preview-11 187 1/23/2024
8.1.2-preview-10 190 1/22/2024
8.1.2-preview-1 177 1/18/2024
8.1.1 247 1/18/2024
8.1.0 198 1/18/2024
8.0.3 288 12/29/2023
8.0.1 241 12/14/2023
8.0.0 278 12/7/2023
6.0.4.3 290 12/29/2023
6.0.4.2 311 12/20/2023
6.0.4.1 296 12/19/2023
6.0.4 291 12/4/2023
6.0.3.20 240 11/27/2023
6.0.3.19 241 11/22/2023