Encamina.Enmarcha.DependencyInjection
8.1.3-preview-01
See the version list below for details.
dotnet add package Encamina.Enmarcha.DependencyInjection --version 8.1.3-preview-01
NuGet\Install-Package Encamina.Enmarcha.DependencyInjection -Version 8.1.3-preview-01
<PackageReference Include="Encamina.Enmarcha.DependencyInjection" Version="8.1.3-preview-01" />
paket add Encamina.Enmarcha.DependencyInjection --version 8.1.3-preview-01
#r "nuget: Encamina.Enmarcha.DependencyInjection, 8.1.3-preview-01"
// Install Encamina.Enmarcha.DependencyInjection as a Cake Addin #addin nuget:?package=Encamina.Enmarcha.DependencyInjection&version=8.1.3-preview-01&prerelease // Install Encamina.Enmarcha.DependencyInjection as a Cake Tool #tool nuget:?package=Encamina.Enmarcha.DependencyInjection&version=8.1.3-preview-01&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.3-preview-01)
- 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 | 820 | 10/22/2024 |
8.2.0-preview-01-m01 | 206 | 9/17/2024 |
8.1.9-preview-03 | 334 | 11/19/2024 |
8.1.9-preview-02 | 170 | 10/22/2024 |
8.1.9-preview-01 | 402 | 10/4/2024 |
8.1.8 | 380 | 9/23/2024 |
8.1.8-preview-07 | 880 | 9/12/2024 |
8.1.8-preview-06 | 352 | 9/11/2024 |
8.1.8-preview-05 | 220 | 9/10/2024 |
8.1.8-preview-04 | 465 | 8/16/2024 |
8.1.8-preview-03 | 279 | 8/13/2024 |
8.1.8-preview-02 | 209 | 8/13/2024 |
8.1.8-preview-01 | 217 | 8/12/2024 |
8.1.7 | 239 | 8/7/2024 |
8.1.7-preview-09 | 242 | 7/3/2024 |
8.1.7-preview-08 | 203 | 7/2/2024 |
8.1.7-preview-07 | 166 | 6/10/2024 |
8.1.7-preview-06 | 178 | 6/10/2024 |
8.1.7-preview-05 | 198 | 6/6/2024 |
8.1.7-preview-04 | 181 | 6/6/2024 |
8.1.7-preview-03 | 203 | 5/24/2024 |
8.1.7-preview-02 | 202 | 5/10/2024 |
8.1.7-preview-01 | 242 | 5/8/2024 |
8.1.6 | 1,627 | 5/7/2024 |
8.1.6-preview-08 | 174 | 5/2/2024 |
8.1.6-preview-07 | 195 | 4/29/2024 |
8.1.6-preview-06 | 498 | 4/26/2024 |
8.1.6-preview-05 | 229 | 4/24/2024 |
8.1.6-preview-04 | 229 | 4/22/2024 |
8.1.6-preview-03 | 191 | 4/22/2024 |
8.1.6-preview-02 | 253 | 4/17/2024 |
8.1.6-preview-01 | 294 | 4/15/2024 |
8.1.5 | 248 | 4/15/2024 |
8.1.5-preview-15 | 210 | 4/10/2024 |
8.1.5-preview-14 | 241 | 3/20/2024 |
8.1.5-preview-13 | 195 | 3/18/2024 |
8.1.5-preview-12 | 213 | 3/13/2024 |
8.1.5-preview-11 | 201 | 3/13/2024 |
8.1.5-preview-10 | 232 | 3/13/2024 |
8.1.5-preview-09 | 206 | 3/12/2024 |
8.1.5-preview-08 | 188 | 3/12/2024 |
8.1.5-preview-07 | 202 | 3/8/2024 |
8.1.5-preview-06 | 358 | 3/8/2024 |
8.1.5-preview-05 | 197 | 3/7/2024 |
8.1.5-preview-04 | 221 | 3/7/2024 |
8.1.5-preview-03 | 214 | 3/7/2024 |
8.1.5-preview-02 | 287 | 2/28/2024 |
8.1.5-preview-01 | 237 | 2/19/2024 |
8.1.4 | 394 | 2/15/2024 |
8.1.3 | 237 | 2/13/2024 |
8.1.3-preview-07 | 175 | 2/13/2024 |
8.1.3-preview-06 | 223 | 2/12/2024 |
8.1.3-preview-05 | 208 | 2/9/2024 |
8.1.3-preview-04 | 204 | 2/8/2024 |
8.1.3-preview-03 | 200 | 2/7/2024 |
8.1.3-preview-02 | 222 | 2/2/2024 |
8.1.3-preview-01 | 171 | 2/2/2024 |
8.1.2 | 262 | 2/1/2024 |
8.1.2-preview-9 | 218 | 1/22/2024 |
8.1.2-preview-8 | 188 | 1/19/2024 |
8.1.2-preview-7 | 181 | 1/19/2024 |
8.1.2-preview-6 | 164 | 1/19/2024 |
8.1.2-preview-5 | 192 | 1/19/2024 |
8.1.2-preview-4 | 175 | 1/19/2024 |
8.1.2-preview-3 | 167 | 1/18/2024 |
8.1.2-preview-2 | 194 | 1/18/2024 |
8.1.2-preview-16 | 205 | 1/31/2024 |
8.1.2-preview-15 | 203 | 1/31/2024 |
8.1.2-preview-14 | 354 | 1/25/2024 |
8.1.2-preview-13 | 177 | 1/25/2024 |
8.1.2-preview-12 | 192 | 1/23/2024 |
8.1.2-preview-11 | 189 | 1/23/2024 |
8.1.2-preview-10 | 192 | 1/22/2024 |
8.1.2-preview-1 | 179 | 1/18/2024 |
8.1.1 | 249 | 1/18/2024 |
8.1.0 | 200 | 1/18/2024 |
8.0.3 | 290 | 12/29/2023 |
8.0.1 | 243 | 12/14/2023 |
8.0.0 | 282 | 12/7/2023 |
6.0.4.3 | 292 | 12/29/2023 |
6.0.4.2 | 313 | 12/20/2023 |
6.0.4.1 | 298 | 12/19/2023 |
6.0.4 | 293 | 12/4/2023 |
6.0.3.20 | 242 | 11/27/2023 |
6.0.3.19 | 245 | 11/22/2023 |