0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

.NET 8 RCにおけるKeyedServiceについて再び

Posted at

以前、「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)

元のリンク:https://mp.weixin.qq.com/s?__biz=MzA3NDM1MzIyMQ==&mid=2247487112&idx=1&sn=f329c0ef0a83574e35369a0354d80d55&chksm=9f0051a2a877d8b4f7a1476040d004449b996298d9e052b3ac775003cc4aa70f052f5985ce71&token=2050451386&lang=zh_CN#rd

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?