aspnetcore 原生 DI 实现基于 key 的服务获取

你可能想通过一个字符串或者其他的类型来获取一个具体的服务实现,那么在 aspnetcore 原生的 MSDI 中,如何实现呢?本文将介绍如何通过自定义工厂来实现。

我们现在恰好有基于 Json 和 MessagePack 的两种序列化器

有一个接口是这样的

public interface ISerializer
{
byte[] Serialize<T>(T obj);
T Deserialize<T>(ReadOnlySpan<byte> data);
}

并且由两个不同的实现

// Json
public class MyJsonSerializer : ISerializer
{
public byte[] Serialize<T>(T obj)
{
throw new NotImplementedException();
}
public T Deserialize<T>(ReadOnlySpan<byte> data)
{
throw new NotImplementedException();
}
}
// MessagePack
public class MyMessagePackSerializer : ISerializer
{
public byte[] Serialize<T>(T obj)
{
throw new NotImplementedException();
}
public T Deserialize<T>(ReadOnlySpan<byte> data)
{
throw new NotImplementedException();
}
}

我有一个服务,需要使用这两种序列化器中的一种。

public class MyService
{
public object DoSomething(string dataType, ReadOnlySpan<byte> data)
{
// 根据 dataType 来决定使用哪种序列化器
}
}

使用委托来定义获取服务的方法

我们可以通过委托来定义获取服务的方法,如下

public delegate ISerializer SerializerFactory(string dataType);

然后在 ConfigureServices 方法中注册

services.AddSingleton<MyJsonSerializer>();
services.AddSingleton<MyMessagePackSerializer>();
services.AddSingleton<SerializerFactory>(sp =>
{
return dataType =>
{
switch (dataType)
{
case "json":
return sp.GetRequiredService<MyJsonSerializer>();
case "msgpack":
return sp.GetRequiredService<MyMessagePackSerializer>();
default:
throw new NotSupportedException();
}
};
});

这样我们就可以在 MyService 中通过委托来获取服务了

public class MyService
{
private readonly SerializerFactory _serializerFactory;
public MyService(SerializerFactory serializerFactory)
{
_serializerFactory = serializerFactory;
}
public object DoSomething(string dataType, ReadOnlySpan<byte> data)
{
var serializer = _serializerFactory(dataType);
return serializer.Deserialize<object>(data);
}
}

基于配置来改变工厂

因为本质是通过委托来获取服务,所以我们可以通过配置来改变委托的行为,如下

public static class SerializerFactoryExtensions
{
public static SerializerFactory CreateSerializerFactory(this IServiceProvider sp)
{
// get mapping from configuration
var mapping = sp.GetRequiredService<IConfiguration>()
.GetSection("SerializerMapping")
.Get<Dictionary<string, string>>();
return dataType =>
{
var serializerType = mapping[dataType];
return (ISerializer)sp.GetRequiredService(Type.GetType(serializerType));
};
}
}

然后在 appsettings.json 中配置

{
"SerializerMapping": {
"json": "WebApplication1.MyJsonSerializer",
"msgpack": "WebApplication1.MyMessagePackSerializer"
}
}

然后在 ConfigureServices 方法中注册

services.AddSingleton<MyJsonSerializer>();
services.AddSingleton<MyMessagePackSerializer>();
services.AddSingleton(SerializerFactoryExtensions.CreateSerializerFactory);

总结

本篇文章介绍了如何通过自定义工厂来实现基于 key 的服务获取,这种方式在 aspnetcore 原生的 DI 中是原生支持的。

参考

感谢阅读,如果觉得本文有用,不妨点击推荐👍或者在评论区留下 Mark,让更多的人可以看到。

欢迎关注作者的微信公众号“newbe技术专栏”,获取更多技术内容。 关注微信公众号“newbe技术专栏”


  1. https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines?WT.mc_id=DX-MVP-5003606

posted @   Newbe36524  阅读(329)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-02-22 使用 Tye 辅助开发 k8s 应用竟如此简单(四)
点击右上角即可分享
微信分享提示