Encamina.Enmarcha.DependencyInjection
8.1.2-preview-12
See the version list below for details.
dotnet add package Encamina.Enmarcha.DependencyInjection --version 8.1.2-preview-12
NuGet\Install-Package Encamina.Enmarcha.DependencyInjection -Version 8.1.2-preview-12
<PackageReference Include="Encamina.Enmarcha.DependencyInjection" Version="8.1.2-preview-12" />
paket add Encamina.Enmarcha.DependencyInjection --version 8.1.2-preview-12
#r "nuget: Encamina.Enmarcha.DependencyInjection, 8.1.2-preview-12"
// Install Encamina.Enmarcha.DependencyInjection as a Cake Addin #addin nuget:?package=Encamina.Enmarcha.DependencyInjection&version=8.1.2-preview-12&prerelease // Install Encamina.Enmarcha.DependencyInjection as a Cake Tool #tool nuget:?package=Encamina.Enmarcha.DependencyInjection&version=8.1.2-preview-12&prerelease
Dependency Injection
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 | Versions 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. |
-
.NETStandard 2.1
- CommunityToolkit.Diagnostics (>= 8.2.2)
- Encamina.Enmarcha.Core (>= 8.1.2-preview-12)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
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.SemanticKernel.Connectors.Document
Package Description |
|
Encamina.Enmarcha.Email.MailKit
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 | 390 | 10/22/2024 |
8.2.0-preview-01-m01 | 189 | 9/17/2024 |
8.1.9-preview-02 | 163 | 10/22/2024 |
8.1.9-preview-01 | 383 | 10/4/2024 |
8.1.8 | 357 | 9/23/2024 |
8.1.8-preview-07 | 624 | 9/12/2024 |
8.1.8-preview-06 | 336 | 9/11/2024 |
8.1.8-preview-05 | 213 | 9/10/2024 |
8.1.8-preview-04 | 455 | 8/16/2024 |
8.1.8-preview-03 | 270 | 8/13/2024 |
8.1.8-preview-02 | 202 | 8/13/2024 |
8.1.8-preview-01 | 212 | 8/12/2024 |
8.1.7 | 224 | 8/7/2024 |
8.1.7-preview-09 | 237 | 7/3/2024 |
8.1.7-preview-08 | 198 | 7/2/2024 |
8.1.7-preview-07 | 159 | 6/10/2024 |
8.1.7-preview-06 | 169 | 6/10/2024 |
8.1.7-preview-05 | 193 | 6/6/2024 |
8.1.7-preview-04 | 176 | 6/6/2024 |
8.1.7-preview-03 | 198 | 5/24/2024 |
8.1.7-preview-02 | 197 | 5/10/2024 |
8.1.7-preview-01 | 237 | 5/8/2024 |
8.1.6 | 1,329 | 5/7/2024 |
8.1.6-preview-08 | 169 | 5/2/2024 |
8.1.6-preview-07 | 190 | 4/29/2024 |
8.1.6-preview-06 | 485 | 4/26/2024 |
8.1.6-preview-05 | 217 | 4/24/2024 |
8.1.6-preview-04 | 220 | 4/22/2024 |
8.1.6-preview-03 | 184 | 4/22/2024 |
8.1.6-preview-02 | 248 | 4/17/2024 |
8.1.6-preview-01 | 285 | 4/15/2024 |
8.1.5 | 239 | 4/15/2024 |
8.1.5-preview-15 | 203 | 4/10/2024 |
8.1.5-preview-14 | 236 | 3/20/2024 |
8.1.5-preview-13 | 190 | 3/18/2024 |
8.1.5-preview-12 | 204 | 3/13/2024 |
8.1.5-preview-11 | 196 | 3/13/2024 |
8.1.5-preview-10 | 225 | 3/13/2024 |
8.1.5-preview-09 | 201 | 3/12/2024 |
8.1.5-preview-08 | 181 | 3/12/2024 |
8.1.5-preview-07 | 197 | 3/8/2024 |
8.1.5-preview-06 | 351 | 3/8/2024 |
8.1.5-preview-05 | 192 | 3/7/2024 |
8.1.5-preview-04 | 216 | 3/7/2024 |
8.1.5-preview-03 | 209 | 3/7/2024 |
8.1.5-preview-02 | 282 | 2/28/2024 |
8.1.5-preview-01 | 232 | 2/19/2024 |
8.1.4 | 387 | 2/15/2024 |
8.1.3 | 224 | 2/13/2024 |
8.1.3-preview-07 | 170 | 2/13/2024 |
8.1.3-preview-06 | 218 | 2/12/2024 |
8.1.3-preview-05 | 199 | 2/9/2024 |
8.1.3-preview-04 | 195 | 2/8/2024 |
8.1.3-preview-03 | 195 | 2/7/2024 |
8.1.3-preview-02 | 215 | 2/2/2024 |
8.1.3-preview-01 | 164 | 2/2/2024 |
8.1.2 | 251 | 2/1/2024 |
8.1.2-preview-9 | 211 | 1/22/2024 |
8.1.2-preview-8 | 183 | 1/19/2024 |
8.1.2-preview-7 | 174 | 1/19/2024 |
8.1.2-preview-6 | 155 | 1/19/2024 |
8.1.2-preview-5 | 187 | 1/19/2024 |
8.1.2-preview-4 | 170 | 1/19/2024 |
8.1.2-preview-3 | 158 | 1/18/2024 |
8.1.2-preview-2 | 189 | 1/18/2024 |
8.1.2-preview-16 | 200 | 1/31/2024 |
8.1.2-preview-15 | 198 | 1/31/2024 |
8.1.2-preview-14 | 345 | 1/25/2024 |
8.1.2-preview-13 | 172 | 1/25/2024 |
8.1.2-preview-12 | 185 | 1/23/2024 |
8.1.2-preview-11 | 184 | 1/23/2024 |
8.1.2-preview-10 | 187 | 1/22/2024 |
8.1.2-preview-1 | 174 | 1/18/2024 |
8.1.1 | 242 | 1/18/2024 |
8.1.0 | 191 | 1/18/2024 |
8.0.3 | 279 | 12/29/2023 |
8.0.1 | 234 | 12/14/2023 |
8.0.0 | 273 | 12/7/2023 |
6.0.4.3 | 283 | 12/29/2023 |
6.0.4.2 | 306 | 12/20/2023 |
6.0.4.1 | 291 | 12/19/2023 |
6.0.4 | 286 | 12/4/2023 |
6.0.3.20 | 235 | 11/27/2023 |
6.0.3.19 | 236 | 11/22/2023 |