.NET 8 RC,再说KeyedService

之前发过一篇《说说KeyedService》,那时的KeyedService用法奇特,还一堆报错,觉得这个功能并不难,即使是在preview版上,也不应该这么低水准,毕竟是大厂出品吗。原来那篇文章如下:

说说KeyedService

桂素伟,公众号:桂迹说说KeyedService

在RC1版,果然修正了这些错误,不管是用Singleton,还是Scoped或Transient,都很完美的支持,使用起来还很丝滑,不管是在Service层还是在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,这里根据配置文件完成短信的通知发送,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配置" } }; ;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值