Use the Type attribute to force a specific injected type, overriding the inferred type from the parameter or member.
using Shouldly;
using Pure.DI;
DI.Setup(nameof(Composition))
.Bind().To<NotificationService>()
// Composition root
.Root<INotificationService>("NotificationService");
var composition = new Composition();
var notificationService = composition.NotificationService;
notificationService.UserNotifier.ShouldBeOfType<EmailSender>();
notificationService.AdminNotifier.ShouldBeOfType<SmsSender>();
interface IMessageSender;
class EmailSender : IMessageSender;
class SmsSender : IMessageSender;
interface INotificationService
{
IMessageSender UserNotifier { get; }
IMessageSender AdminNotifier { get; }
}
class NotificationService(
// The [Type] attribute forces the injection of a specific type,
// overriding the default resolution behavior.
[Type(typeof(EmailSender))] IMessageSender userNotifier,
[Type(typeof(SmsSender))] IMessageSender adminNotifier)
: INotificationService
{
public IMessageSender UserNotifier { get; } = userNotifier;
public IMessageSender AdminNotifier { get; } = adminNotifier;
}Running this code sample locally
- Make sure you have the .NET SDK 10.0 or later installed
dotnet --list-sdk- Create a net10.0 (or later) console application
dotnet new console -n Sampledotnet add package Pure.DI
dotnet add package Shouldly- Copy the example code into the Program.cs file
You are ready to run the example 🚀
dotnet runThe Type attribute is part of the API, but you can define your own in any assembly or namespace.
The following partial class will be generated:
partial class Composition
{
public INotificationService NotificationService
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return new NotificationService(new EmailSender(), new SmsSender());
}
}
}Class diagram:
---
config:
maxTextSize: 2147483647
maxEdges: 2147483647
class:
hideEmptyMembersBox: true
---
classDiagram
NotificationService --|> INotificationService
Composition ..> NotificationService : INotificationService NotificationService
NotificationService *-- EmailSender : EmailSender
NotificationService *-- SmsSender : SmsSender
namespace Pure.DI.UsageTests.Attributes.TypeAttributeScenario {
class Composition {
<<partial>>
+INotificationService NotificationService
}
class EmailSender {
<<class>>
+EmailSender()
}
class INotificationService {
<<interface>>
}
class NotificationService {
<<class>>
+NotificationService(IMessageSender userNotifier, IMessageSender adminNotifier)
}
class SmsSender {
<<class>>
+SmsSender()
}
}