.Net 一套接口多实现
接口(interface)可理解为规范、标准、协议。接口是用来约束各方都在同一组规范下工作。
电脑外设USB接口,各个品牌商家生产的U盘、鼠标都能够被电脑主板识别并工作,这是因为个生产商都遵循实现了USB接口协议。
在编程中接口应用非常广泛,例如IDbConnection接口,这是一组数据库连接的接口,由各个数据库驱动实现,因此.Net可以操作多种数据库。
一套接口多实现的基本结构如下

实现思路是,通过在各实现类上使用Attribute进行标记,然后定义一个实例获取类,通过反射获取所有实现该接口并且标记了的实现类,并将各个实现类通过IOC注册,然后通过标记的类型获取对应的实现类。
接下来是demo演示
定义服务标记- [AttributeUsageAttribute(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
- public class MultiServiceTypeAttribute : Attribute
- {
- public string ServiceType { get; private set; }
- public MultiServiceTypeAttribute(string serviceType)
- {
- ServiceType = serviceType;
- }
- }
复制代码 定义接口IMultiInterface- public interface IMultiInterface
- {
- void Do();
- }
复制代码 定义实现类AMultiInterfaceImplA并标记- [MultiServiceTypeAttribute("A")]
- public class MultiInterfaceImplA : IMultiInterface
- {
- public void Do()
- {
- Console.WriteLine("这是A实现的调用");
- }
- }
复制代码 定义实现类BMultiInterfaceImplB并标记- [MultiServiceTypeAttribute("B")]
- public class MultiInterfaceImplB : IMultiInterface
- {
- public void Do()
- {
- Console.WriteLine("这是B实现的调用");
- }
- }
复制代码 将接口与实现添加到IOC容器,这里使用 Microsoft.Extensions.DependencyInjection.dll和Microsoft.Extensions.DependencyInjection.Abstractions.dll两个库来实现简易IOC容器- public class ServiceLoader
- {
- private readonly ServiceCollection __ioc = new ServiceCollection();
- private ServiceProvider __iocServiceProvider;
- private static object _lock = new object();
- private static ServiceLoader _inst;
- public static ServiceLoader Inst
- {
- get
- {
- if (_inst == null)
- {
- lock (_lock)
- {
- if (_inst == null)
- {
- _inst = new ServiceLoader();
- _inst.Init();
- }
- }
- }
- return _inst;
- }
- }
- private void Init()
- {
- var tps = typeof(IMultiInterface).Assembly.GetTypes().Where(x =>
- x.GetInterfaces().Any(_ => _.Name == nameof(IMultiInterface)));
- foreach (var item in tps)
- {
- if (item.IsClass)
- {
- Inst.AddTransient(typeof(IMultiInterface), item);
- }
- }
- Interlocked.Exchange(ref __iocServiceProvider, Inst.__ioc.BuildServiceProvider());
- }
- private void AddTransient(Type iface, Type impl)
- {
- __ioc.AddTransient(iface, impl);
- }
- }
复制代码 根据标记的类型获取对应的接口实现。在ServiceLoader中继续添加以下方法- public IMultiInterface GetService(string serviceType)
- {
- var svcList = __iocServiceProvider.GetServices<IMultiInterface>();
- var svc = svcList.FirstOrDefault(x => x.GetType().GetCustomAttribute<MultiServiceTypeAttribute>()?.ServiceType == serviceType);
- if (svc == null)
- {
- //Console.WriteLine($@"未找到 {serviceType} 服务实现,使用默认实现");
- // 如果有默认实现类,这里可选择调用默认实现
- //svc = svcList.FirstOrDefault(x => x.GetType().GetCustomAttribute<MultiServiceTypeAttribute>()?.ServiceType == "__default__");
- throw new Exception($"未找到 {serviceType} 服务实现");
- }
- return svc;
- }
复制代码 通过ServiceLoader.Inst.GetService("serviceType").Do();来获取对应的接口实例,入参就是各个实现类上标记的类型,并调用接口。
调用示例如下

至此实现了一接口多实现的雏形。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |