以前、「KeyedServiceについて」という記事を公開しましたが、当時のKeyedServiceの使用法は奇抜であり、様々なエラーが発生していました。このような機能はそれほど難しいものではないはずで、プレビュー版であってもこのような低品質であってはならないと感じました。結局、大手企業が開発したものですから。以前の記事は以下の通りです:
KeyedServiceについて
桂素伟氏、公式アカウント:桂跡
RC1バージョンでは、これらのエラーがしっかり修正されました。Singleton、Scoped、Transientを使用するかに関わらず、どれも完璧にサポートされ、サービス層やMapメソッド内での使用でも非常にスムーズです。[FromKeyedServices("名称")]は問題なくサポートされています。一部の友人が言うように、この機能の登場は少し遅れましたが、遅れて登場することが全くないよりはずっと良いです。この機能を使用する開発者は、コードを更新して.NET8.0に移行する準備ができているでしょう。長期サポートバージョンですから。
using Microsoft.Extensions.DependencyInjection.Extensions;
var builder = WebApplication.CreateBuilder(args);
//builder.Services.AddKeyedSingleton<IConfigRepository, ISMSConfigRepository>("smsRep");//builder.Services.AddKeyedSingleton<INotifyService, SMSService>("smsSev");
//builder.Services.AddKeyedSingleton<IConfigRepository, IEMailConfigRepository>("emailRep");//builder.Services.AddKeyedSingleton<INotifyService, EMailService>("emailSev");
//builder.Services.AddKeyedScoped<IConfigRepository, ISMSConfigRepository>("smsRep");//builder.Services.AddKeyedScoped<INotifyService, SMSService>("smsSev");
//builder.Services.AddKeyedScoped<IConfigRepository, IEMailConfigRepository>("emailRep");//builder.Services.AddKeyedScoped<INotifyService, EMailService>("emailSev");
builder.Services.AddKeyedTransient<IConfigRepository, ISMSConfigRepository>("smsRep");
builder.Services.AddKeyedTransient<INotifyService, SMSService>("smsSev");
builder.Services.AddKeyedTransient<IConfigRepository, IEMailConfigRepository>("emailRep");
builder.Services.AddKeyedTransient<INotifyService, EMailService>("emailSev");
var app = builder.Build();
app.MapGet("/email", ([FromKeyedServices("emailSev")] INotifyService notifyService, string message) => new
{
Result = notifyService.Notify(message),
Messgae = message,
Type = notifyService.GetType().Name
});
app.MapGet("/sms", ([FromKeyedServices("smsSev")] INotifyService notifyService, string message) => new
{
Result = notifyService.Notify(message),
Messgae = message,
Type = notifyService.GetType().Name
});
app.Run();
public interface INotifyService
{
bool Notify(string message);
}
public class SMSService : INotifyService
{
private readonly Dictionary<string, dynamic> _configs;
private readonly ILogger<EMailService> _logger;
public SMSService([FromKeyedServices("smsRep")] IConfigRepository configRepository, ILogger<EMailService> logger)
{
_logger = logger;
_configs = configRepository.GetConfig();
}
public bool Notify(string message)
{
_logger.LogInformation($"{_configs["name"]},SMSService,この設定ファイルに基づいてSMS通知送信を完了,Message:{message}");
return true;
}
}
public class EMailService : INotifyService
{
private readonly Dictionary<string, dynamic> _configs;
private readonly ILogger<EMailService> _logger;
public EMailService([FromKeyedServices("emailRep")] IConfigRepository configRepository, ILogger<EMailService> logger)
{
_logger = logger;
_configs = configRepository.GetConfig();
}
public bool Notify(string message)
{
_logger.LogInformation($"{_configs["name"]},EMailService,この設定ファイルに基づいてメール通知送信を完了,Message:{message}");
return true;
}
}
public interface IConfigRepository
{
Dictionary<string, dynamic> GetConfig();
}
public class IEMailConfigRepository : IConfigRepository
{
public Dictionary<string, dynamic> GetConfig()
{
//データベースから設定情報を取得
return new Dictionary<string, dynamic>() { { "name", "email設定" } };
}
}
public class ISMSConfigRepository : IConfigRepository
{
public Dictionary<string, dynamic> GetConfig()
{
//データベースから設定情報を取得
return new Dictionary<string, dynamic>() { { "name", "sms設定" } }; ;
}
}
(Translated by GPT)