在netcore中如何注入同一个接口的多个实现
netcore中自带了Ioc框架,这也影响了我们的编码习惯,以前都是静态类或者直接new对象,现在有了Ioc框架的支持,我们也不必守旧,应当使用起来,接受这种对象管理方式。使用过java的同仁,都习惯了Spring,感觉离开了Spring就好像失去了灵魂一样。Spring经过多年的沉淀,非常的稳定和灵活,相比之下,netcore中自带的Ioc框架太过轻量,中规中矩的使用还算够用,但是其灵活性的确有待加强。比如属性注入、字段注入、方法参数注入等等,虽然说官方强烈建议使用构造函数注入,并且只提供了构造函数注入,但是我感觉后期肯定会支持更多都注入方法的,并且会结合Attribute提供更为灵活的功能,期望这一天能快点到来!
今天主要讨论其中一个问题,就是如何在netcore中注入同一个接口的多个实现,注册多个实现并不难,关键是如何优雅的取出来。下面我们用代码来说话:
public interface IServiceA
{
string GetStr();
}
public class ImplA1 : IServiceA
{
public string GetStr()
{
return "ImplA1";
}
}
public class ImplA2 : IServiceA
{
public string GetStr()
{
return "ImplA2";
}
}
上面的代码是有一个服务接口IServieA,和两个不同的实现类。如果我们这样注册:
services.AddSingleton<IServiceA>(new ImplA1());
services.AddSingleton<IServiceA>(new ImplA2());
第一种使用方式:
public class HomeController : Controller
{
private readonly IServiceA serviceA;
public HomeController(IServiceA serviceA)
{
this.serviceA = serviceA; //这里注入进来的是最后一个实现,也就是ImplA2
}
public IActionResult Index()
{
return Content(serviceA.GetStr());
}
}
这种方法肯定是不行的,因为只能获取注册的最后一个实现。
第二种使用方式:
public class HomeController : Controller
{
private readonly IServiceA serviceA;
public HomeController(IEnumerable<IServiceA> serviceAList)
{
this.serviceA = serviceAList.First(); //这里注入进来的是一个服务集合
}
public IActionResult Index()
{
return Content(serviceA.GetStr());
}
}
这种方法也不好,需要自己在集合中筛选要想的实现。
其实我们更希望是这样的,注册的时候可以使用一个标识,然后使用Attribute指定标识来获取具体的实现:
services.AddSingleton<IServiceA>("impla1",new ImplA1()); //impla1为该实现在该接口的唯一标识
services.AddSingleton<IServiceA>("impla2",new ImplA2()); //同上
可能会有这么一个Attribute类:
public class IdentifierAttribute : Attribute
{
public IdentifierAttribute(string id)
{
this.Id = id;
}
public string Id { get; set; }
}
然后使用的时候大概是这样:
public HomeController([Identifier("impla1")]IServiceA serviceA)
{
this.serviceA = serviceA; //根据Identifier的指定,这里应该是ImplA1,
}
如果能像Spring中@Autowired注解,注入字段就更好了:
[Autowired]
[Identifier("impla1")]
private readonly IServiceA serviceA;
然而,我们该醒醒了,netcore中自带的Ioc没有提供类似这样的功能,我找了很久也没找到。
作为程序员,我们都要具备一种特质,那就是爱思考,经过思考,我感觉可以使用工厂来解决这个问题。这里只是提供一种思路和实现,有更好的方法大家可以留言讨论。
首先我们定义一个用于存储单例的集合类:
public class SingletonFactory
{
Dictionary<Type, Dictionary<string, object>> serviceDict;
public SingletonFactory()
{
serviceDict = new Dictionary<Type, Dictionary<string, object>>();
}
public TService GetService<TService>(string id) where TService : class
{
var serviceType = typeof(TService);
return GetService<TService>(serviceType, id);
}
public TService GetService<TService>(Type serviceType, string id) where TService : class
{
if (serviceDict.TryGetValue(serviceType, out Dictionary<string, object> implDict))
{
if (implDict.TryGetValue(id, out object service))
{
return service as TService;
}
}
return null;
}
public void AddService<TService>(TService service, string id) where TService : class
{
AddService(typeof(TService), service, id);
}
public void AddService(Type serviceType, object service, string id)
{
if (service != null)
{
if (serviceDict.TryGetValue(serviceType, out Dictionary<string, object> implDict))
{
implDict[id] = service;
}
else
{
implDict = new Dictionary<string, object>();
implDict[id] = service;
serviceDict[serviceType] = implDict;
}
}
}
}
这个类中使用一个嵌套的字典来存储服务类的实现,提供添加服务和获取服务的方法。
有了这样一个类,在netcore自带的Ioc中我们就可以获取更多的控制权了。且看下面代码。
注册服务:
SingletonFactory singletonFactory = new SingletonFactory();
singletonFactory.AddService<IServiceA>(new ImplA1(), "impla1");
singletonFactory.AddService<IServiceA>(new ImplA2(), "impla2");
services.AddSingleton(singletonFactory);
我们把同一个接口的多个实现都加到SingletonFactory中,然后把SingletonFactory注册到Ioc。
使用服务:
private readonly IServiceA serviceA;
public HomeController(SingletonFactory singletonFactory)
{
this.serviceA = singletonFactory.GetService<IServiceA>("impla2"); //使用标识从SingletonFactory获取自己想要的服务实现
}
使用服务的时候我们注入SingletonFactory,然后从SingletonFactory根据标识获取具体的服务实现。
这个方法还算优雅吗?
在netcore中如何注入同一个接口的多个实现的更多相关文章
- SpringBoot JPA 中无法注入 JpaRepository 接口的问题及解决方案
错误: 在Springboot 框架中使用JPA的过程中,怎么来实现数据库操作底层的交互呢?Spring JPA其实已经提供了一套很全面的解决方案,实现对数据库的增.删.查.改只需要继承JPA实现类 ...
- Spring Boot + JPA 多模块项目无法注入 JpaRepository 接口
问题描述 Spring Boot + JPA 多模块项目,启动报异常: nested exception is org.springframework.beans.factory.NoSuchBean ...
- .netcore中的依赖注入
IOC.DI相关概念的理解 1.依赖:简单的讲就是"引用到".例如AccountController.cs引用到IAccountService.cs,那么AccountContro ...
- .NetCore中三种注入方式的思考
该篇内容由个人博客点击跳转同步更新!转载请注明出处! .NetCore彻底诠释了"万物皆可注入"这句话的含义,在.NetCore中到处可见注入的使用.因此core中也提供了三种注入 ...
- 由一个RABBITMQ监听器死循环引出的SPRING中BEAN和MAPPER接口的注入问题
1 @Slf4j 2 @RestController 3 @Component 4 public class VouchersReceiverController implements Message ...
- Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Ocelot+Polly缓存、限流、熔断、降级
相关文章 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服务注册,服务发现 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-网 ...
- .NetCore中的日志(2)集成第三方日志工具
.NetCore中的日志(2)集成第三方日志工具 0x00 在.NetCore的Logging组件中集成NLog 上一篇讨论了.NetCore中日志框架的结构,这一篇讨论一下.NetCore的Logg ...
- .NetCore中的日志(1)日志组件解析
.NetCore中的日志(1)日志组件解析 0x00 问题的产生 日志记录功能在开发中很常用,可以记录程序运行的细节,也可以记录用户的行为.在之前开发时我一般都是用自己写的小工具来记录日志,输出目标包 ...
- spring容器注入一个接口的两个实现类
spring容器中能拥有两个同种类型的bean吗?我有两个dao类同时实现一个接口,这两个接口注入时报了异常如下. org.springframework.beans.factory.NoSuchBe ...
随机推荐
- 列式数据库~clickhouse 场景以及安装
一 简介:列式数据库clickhouse的安装与基本操作二 基本介绍:ClickHouse来自俄罗斯,是一款列式数据库三 适用场景: 简单类型的大数据统计四 限制 1 不支持更新操作,不支持事 ...
- SpringBoot2.x过滤器Filter和使用Servlet3.0配置自定义Filter实战
补充:SpringBoot启动日志 1.深入SpringBoot2.x过滤器Filter和使用Servlet3.0配置自定义Filter实战(核心知识) 简介:讲解SpringBoot里面Filter ...
- 一套oracle的练习题
create table student( sno varchar2(10) primary key, sname varchar2(20), sage number(2), ssex varchar ...
- 一份通过IPC$和lpk.dll感染方式的病毒分析报告
样本来自52pojie论坛,从事过两年渗透开始学病毒分析后看到IPC$真是再熟悉不过. 1.样本概况 1.1 样本信息 病毒名称:3601.exe MD5值:96043b8dcc7a977b16a28 ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
- sync_binlog innodb_flush_log_at_trx_commit 浅析【转】
innodb_flush_log_at_trx_commit和sync_binlog 两个参数是控制MySQL 磁盘写入策略以及数据安全性的关键参数.本文从参数含义,性能,安全角度阐述两个参数为不同的 ...
- openwrt 中route配置
route配置项默认保存在文件 /etc/config/network 中. 配置route的接口“interface” 使用的协议需要为dhcp才可. config interface 'wan' ...
- 解决报错error the @annotation pointcut expression is only supported at Java 5
eclipse搭建环境后报错 error the @annotation pointcut expression is only supported at Java 5 错误意思大致是:注释切入点表达 ...
- Nodejs实现WebSocket通信demo
一.创建websocket.js文件 步骤: 1.创建websocket.js文件,复制如下代码: 2.安装nodejs-websocket依赖: 3.该文件夹下命令行执行 node websocke ...
- 目标检测--Spatial pyramid pooling in deep convolutional networks for visual recognition(PAMI, 2015)
Spatial pyramid pooling in deep convolutional networks for visual recognition 作者: Kaiming He, Xiangy ...