net core天马行空系列: 一个接口多个实现类,利用mixin技术通过自定义服务名,实现精准属性注入
系列目录
1.net core天马行空系列:原生DI+AOP实现spring boot注解式编程
2.net core天马行空系列: 泛型仓储和声明式事物实现最优雅的crud操作
哈哈哈哈,大家好,我就是高产似母猪的三合。日常开发中,我们常会遇到这样的场景,一个接口,有多个实现类,在某个业务中,我们希望指定某个实现类,如今网络上常见的解决方案,就是注入一个委托或者利用工厂模式,这些方式虽然能实现功能,但使用起来还是不够优雅,如何才称得上优雅呢?自然是在添加服务的时候给服务指定名称,注入的时候根据名称来进行注入,没错,就是类似spring boot里的@Qualifier注解,那么net core能实现这个注解么?要知道net core里的原生DI并不支持在注册的时候给服务添加名称,所以实现起来也是难上加难,但是,利用动态代理,我们就能完美解决这个问题,没错,他Lei了,net core版[QualifierAttribute],实现这个注解用到的最核心的技术,就是动态代理中非常重要的mixin,什么是mixin呢?玩过less的小伙伴可能会知道,这玩意可以把2个css样式混合成一个,而在net core中,DP(castle.dynamicproxy)提供的mixin技术,可以在程序运行时,把多个类混合成一个动态代理类,这个混合类既可以用作类1,又可以用作类2,那么思路就很清晰了,类1就是我们注入的服务类,类2需要我们自定义,在我们的这个自定义类中,我们可以添加一个Name的属性,这样在注册服务的时候给服务命名,到了注入的时候,我们就可以根据名称来进行精准地的属性注入。BTW,动态代理,注解式编程,AOP贯穿本系列始终,no bb,正文开始。
1.定义用到的类
定义QualifierAttribute注解
public class QualifierAttribute : Attribute
{
public string Name { get; } public QualifierAttribute(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("服务名称不能为空");
} this.Name = name;
}
}
定义用来进行混合的附加类接口IDependencyAddition和他的实现类DependencyAddition,类里面只有一个名称的属性。
public interface IDependencyAddition
{
string Name { set; get; }
}
public class DependencyAddition : IDependencyAddition
{
public string Name { set; get; }
}
第一篇文章里,讲过有一辆汽车,汽车里有引擎,我们在这基础上进行扩展,汽车总是有轮胎的,我们定义一个轮胎接口IWheel和他的2个实现类WheelA和WheelB,类里只有一个Scroll方法,表示轮胎在滚,同时向控制台输出他们是哪种轮胎。
public interface IWheel
{
void Scroll();
}
public class WheelA : IWheel
{
public void Scroll()
{
Console.WriteLine("我是A轮胎,我正在滚");
}
}
public class WheelB : IWheel
{
public void Scroll()
{
Console.WriteLine("我是B轮胎,我正在滚");
}
}
修改汽车类接口ICar和他的实现类Car,主要就是添加了轮胎的属性注入,并且用[Qualifier("B轮胎")]注解指定使用哪种轮胎,然后调用Scroll方法,让轮胎滚起来。
public interface ICar
{
Engine Engine { set; get; } IWheel Wheel { set; get; } void Fire();
}
public class Car : ICar
{
[Autowired]
public Engine Engine { set; get; } [Value("oilNo")]
public int OilNo { set; get; } [Autowired]
[Qualifier("B轮胎")]
public IWheel Wheel { set; get; } [Transactional]
public void Fire()
{
Console.WriteLine("加满" + OilNo + "号汽油,点火"); Wheel.Scroll(); Engine.Start();
}
}
IServiceCollection的静态扩展类SummerBootExtentions,和上一篇相比,主要是添加了AddSbService的新重载,使注册服务的时候能够为服务添加名称,在这基础上进一步封装了AddSbSingleton,AddSbScoped和AddSbTransient方法。还有在工厂委托函数里进行属性注入的时候,添加了支持[QualifierAttribute]注解的逻辑,完整类代码如下:
public static class SummerBootExtentions
{
/// <summary>
/// 瞬时
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="services"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbTransient<TService, TImplementation>(this IServiceCollection services, params Type[] interceptorTypes)
{
return services.AddSbService(typeof(TService), typeof(TImplementation), ServiceLifetime.Transient, interceptorTypes);
} /// <summary>
/// 瞬时
/// </summary>
/// <param name="services"></param>
/// <param name="serviceType"></param>
/// <param name="implementationType"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbTransient(this IServiceCollection services, Type serviceType,
Type implementationType, params Type[] interceptorTypes)
{
return services.AddSbService(serviceType, implementationType, ServiceLifetime.Transient, interceptorTypes);
} /// <summary>
/// 请求级别
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="services"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbScoped<TService, TImplementation>(this IServiceCollection services, params Type[] interceptorTypes)
{
return services.AddSbService(typeof(TService), typeof(TImplementation), ServiceLifetime.Scoped, interceptorTypes);
} /// <summary>
/// 请求级别
/// </summary>
/// <param name="services"></param>
/// <param name="serviceType"></param>
/// <param name="implementationType"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbScoped(this IServiceCollection services, Type serviceType,
Type implementationType, params Type[] interceptorTypes)
{
return services.AddSbService(serviceType, implementationType, ServiceLifetime.Scoped, interceptorTypes);
} /// <summary>
/// 单例
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="services"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbSingleton<TService, TImplementation>(this IServiceCollection services, params Type[] interceptorTypes)
{
return services.AddSbService(typeof(TService), typeof(TImplementation), ServiceLifetime.Singleton, interceptorTypes);
} /// <summary>
/// 单例
/// </summary>
/// <param name="services"></param>
/// <param name="serviceType"></param>
/// <param name="implementationType"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbSingleton(this IServiceCollection services, Type serviceType,
Type implementationType, params Type[] interceptorTypes)
{
return services.AddSbService(serviceType, implementationType, ServiceLifetime.Singleton, interceptorTypes);
} public static IServiceCollection AddSbService(this IServiceCollection services, Type serviceType, Type implementationType,
ServiceLifetime lifetime, params Type[] interceptorTypes)
{
services.Add(new ServiceDescriptor(implementationType, implementationType, lifetime)); object Factory(IServiceProvider provider)
{
var target = provider.GetService(implementationType);
var properties = implementationType.GetTypeInfo().DeclaredProperties; foreach (PropertyInfo info in properties)
{
//属性注入
if (info.GetCustomAttribute<AutowiredAttribute>() != null)
{
var propertyType = info.PropertyType;
object impl = null;
var qualifierAttribute = info.GetCustomAttribute<QualifierAttribute>();
if (qualifierAttribute != null)
{
var serviceName = qualifierAttribute.Name;
var implList = provider.GetServices(propertyType);
foreach (var tmpImpl in implList)
{
if (ProxyUtil.IsProxy(tmpImpl))
{
var addition = tmpImpl as IDependencyAddition;
if (addition?.Name == serviceName)
{
impl = tmpImpl;
break; ;
}
}
}
}
else
{
impl = provider.GetService(propertyType);
} if (impl != null)
{
info.SetValue(target, impl);
}
} //配置值注入
if (info.GetCustomAttribute<ValueAttribute>() is ValueAttribute valueAttribute)
{
var value = valueAttribute.Value;
if (provider.GetService(typeof(IConfiguration)) is IConfiguration configService)
{
var pathValue = configService.GetSection(value).Value;
if (pathValue != null)
{
var pathV = Convert.ChangeType(pathValue, info.PropertyType);
info.SetValue(target, pathV);
}
} }
} List<IInterceptor> interceptors = interceptorTypes.ToList()
.ConvertAll<IInterceptor>(interceptorType => provider.GetService(interceptorType) as IInterceptor); var proxyGenerator = provider.GetService<ProxyGenerator>();
var proxy = proxyGenerator.CreateInterfaceProxyWithTarget(serviceType, target, interceptors.ToArray()); return proxy;
}; var serviceDescriptor = new ServiceDescriptor(serviceType, Factory, lifetime);
services.Add(serviceDescriptor); return services;
} /// <summary>
/// 瞬时
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="services"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbTransient<TService>(this IServiceCollection services, params Type[] interceptorTypes)
{
return services.AddSbService(typeof(TService), ServiceLifetime.Transient, interceptorTypes);
} /// <summary>
/// 瞬时
/// </summary>
/// <param name="services"></param>
/// <param name="serviceType"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbTransient(this IServiceCollection services, Type serviceType, params Type[] interceptorTypes)
{
return services.AddSbService(serviceType, ServiceLifetime.Transient, interceptorTypes);
} /// <summary>
/// 请求
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="services"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbScoped<TService>(this IServiceCollection services, params Type[] interceptorTypes)
{
return services.AddSbService(typeof(TService), ServiceLifetime.Scoped, interceptorTypes);
} /// <summary>
/// 请求
/// </summary>
/// <param name="services"></param>
/// <param name="serviceType"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbScoped(this IServiceCollection services, Type serviceType,
params Type[] interceptorTypes)
{
return services.AddSbService(serviceType, ServiceLifetime.Scoped, interceptorTypes);
} /// <summary>
/// 单例
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="services"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbSingleton<TService>(this IServiceCollection services, params Type[] interceptorTypes)
{
return services.AddSbService(typeof(TService), ServiceLifetime.Singleton, interceptorTypes);
} /// <summary>
/// 单例
/// </summary>
/// <param name="services"></param>
/// <param name="serviceType"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbSingleton(this IServiceCollection services, Type serviceType, params Type[] interceptorTypes)
{
return services.AddSbService(serviceType, ServiceLifetime.Singleton, interceptorTypes);
} public static IServiceCollection AddSbService(this IServiceCollection services, Type serviceType,
ServiceLifetime lifetime, params Type[] interceptorTypes)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
if (serviceType == (Type)null)
throw new ArgumentNullException(nameof(serviceType)); object Factory(IServiceProvider provider)
{
List<IInterceptor> interceptors = interceptorTypes.ToList()
.ConvertAll<IInterceptor>(interceptorType => provider.GetService(interceptorType) as IInterceptor); var proxyGenerator = provider.GetService<ProxyGenerator>(); var proxy = proxyGenerator.CreateClassProxy(serviceType, interceptors.ToArray()); var properties = serviceType.GetTypeInfo().DeclaredProperties; foreach (PropertyInfo info in properties)
{
//属性注入
if (info.GetCustomAttribute<AutowiredAttribute>() != null)
{
var propertyType = info.PropertyType;
object impl = null;
var qualifierAttribute = info.GetCustomAttribute<QualifierAttribute>();
if (qualifierAttribute != null)
{
var serviceName = qualifierAttribute.Name;
var implList = provider.GetServices(propertyType);
foreach (var tmpImpl in implList)
{
if (ProxyUtil.IsProxy(tmpImpl))
{
var addition = tmpImpl as IDependencyAddition;
if (addition?.Name == serviceName)
{
impl = tmpImpl;
break; ;
}
}
}
}
else
{
impl = provider.GetService(propertyType);
} if (impl != null)
{
info.SetValue(proxy, impl);
}
}
//配置值注入
if (info.GetCustomAttribute<ValueAttribute>() is ValueAttribute valueAttribute)
{
var value = valueAttribute.Value;
if (provider.GetService(typeof(IConfiguration)) is IConfiguration configService)
{
var pathValue = configService.GetSection(value).Value;
if (pathValue != null)
{
var pathV = Convert.ChangeType(pathValue, info.PropertyType);
info.SetValue(proxy, pathV);
}
}
}
} return proxy;
}; var serviceDescriptor = new ServiceDescriptor(serviceType, Factory, lifetime);
services.Add(serviceDescriptor); return services;
} /// <summary>
/// 添加summer boot扩展
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IMvcBuilder AddSB(this IMvcBuilder builder)
{
if (builder == null)
throw new ArgumentNullException(nameof(builder));
ControllerFeature feature = new ControllerFeature();
builder.PartManager.PopulateFeature<ControllerFeature>(feature);
foreach (Type type in feature.Controllers.Select<TypeInfo, Type>((Func<TypeInfo, Type>)(c => c.AsType())))
builder.Services.TryAddTransient(type, type);
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, SbControllerActivator>()); return builder;
} public static IServiceCollection AddSbRepositoryService(this IServiceCollection services, params Type[] interceptorTypes)
{
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(it => it.GetTypes()); var tableType = types.Where(it => it.GetCustomAttribute<TableAttribute>() != null); foreach (var type in tableType)
{
var injectServiceType = typeof(IRepository<>).MakeGenericType(type);
var injectImplType = typeof(BaseRepository<>).MakeGenericType(type);
services.AddSbScoped(injectServiceType, injectImplType, interceptorTypes);
} return services;
} /// <summary>
/// 瞬时
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="services"></param>
/// <param name="name"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbTransient<TService, TImplementation>(this IServiceCollection services, string name = "", params Type[] interceptorTypes)
{
return services.AddSbService(typeof(TService), typeof(TImplementation), ServiceLifetime.Transient, name, interceptorTypes);
} /// <summary>
/// 瞬时
/// </summary>
/// <param name="services"></param>
/// <param name="serviceType"></param>
/// <param name="implementationType"></param>
/// <param name="name"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbTransient(this IServiceCollection services, Type serviceType,
Type implementationType, string name = "", params Type[] interceptorTypes)
{
return services.AddSbService(serviceType, implementationType, ServiceLifetime.Transient, name, interceptorTypes);
} /// <summary>
/// 请求级别
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="services"></param>
/// <param name="name"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbScoped<TService, TImplementation>(this IServiceCollection services, string name = "", params Type[] interceptorTypes)
{
return services.AddSbService(typeof(TService), typeof(TImplementation), ServiceLifetime.Scoped, name, interceptorTypes);
} /// <summary>
/// 请求级别
/// </summary>
/// <param name="services"></param>
/// <param name="serviceType"></param>
/// <param name="implementationType"></param>
/// <param name="name"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbScoped(this IServiceCollection services, Type serviceType,
Type implementationType, string name = "", params Type[] interceptorTypes)
{
return services.AddSbService(serviceType, implementationType, ServiceLifetime.Scoped, name, interceptorTypes);
} /// <summary>
/// 单例
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="services"></param>
/// <param name="name"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbSingleton<TService, TImplementation>(this IServiceCollection services, string name = "", params Type[] interceptorTypes)
{
return services.AddSbService(typeof(TService), typeof(TImplementation), ServiceLifetime.Singleton, name, interceptorTypes);
} /// <summary>
/// 单例
/// </summary>
/// <param name="services"></param>
/// <param name="serviceType"></param>
/// <param name="implementationType"></param>
/// <param name="name"></param>
/// <param name="interceptorTypes"></param>
/// <returns></returns>
public static IServiceCollection AddSbSingleton(this IServiceCollection services, Type serviceType,
Type implementationType, string name = "", params Type[] interceptorTypes)
{
return services.AddSbService(serviceType, implementationType, ServiceLifetime.Singleton, name, interceptorTypes);
} public static IServiceCollection AddSbService(this IServiceCollection services, Type serviceType, Type implementationType,
ServiceLifetime lifetime, string name = "", params Type[] interceptorTypes)
{
services.Add(new ServiceDescriptor(implementationType, implementationType, lifetime)); object Factory(IServiceProvider provider)
{
var target = provider.GetService(implementationType);
var properties = implementationType.GetTypeInfo().DeclaredProperties; foreach (PropertyInfo info in properties)
{
//属性注入
if (info.GetCustomAttribute<AutowiredAttribute>() != null)
{
var propertyType = info.PropertyType;
object impl = null;
var qualifierAttribute = info.GetCustomAttribute<QualifierAttribute>();
if (qualifierAttribute != null)
{
var serviceName = qualifierAttribute.Name;
var implList = provider.GetServices(propertyType);
foreach (var tmpImpl in implList)
{
if (ProxyUtil.IsProxy(tmpImpl))
{
var addition = tmpImpl as IDependencyAddition;
if (addition?.Name == serviceName)
{
impl = tmpImpl;
break; ;
}
}
}
}
else
{
impl = provider.GetService(propertyType);
} if (impl != null)
{
info.SetValue(target, impl);
}
} //配置值注入
if (info.GetCustomAttribute<ValueAttribute>() is ValueAttribute valueAttribute)
{
var value = valueAttribute.Value;
if (provider.GetService(typeof(IConfiguration)) is IConfiguration configService)
{
var pathValue = configService.GetSection(value).Value;
if (pathValue != null)
{
var pathV = Convert.ChangeType(pathValue, info.PropertyType);
info.SetValue(target, pathV);
}
} }
} List<IInterceptor> interceptors = interceptorTypes.ToList()
.ConvertAll<IInterceptor>(interceptorType => provider.GetService(interceptorType) as IInterceptor); var proxyGenerator = provider.GetService<ProxyGenerator>();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new DependencyAddition() { Name = name });
var proxy = proxyGenerator.CreateInterfaceProxyWithTarget(serviceType, target, options, interceptors.ToArray()); return proxy;
}; var serviceDescriptor = new ServiceDescriptor(serviceType, Factory, lifetime);
services.Add(serviceDescriptor); return services;
}
}
修改第一篇中自定义的控制器激活类SbControllerActivator,添加支持[QualifierAttribute]注解的逻辑。
public class SbControllerActivator : IControllerActivator
{
/// <inheritdoc />
public object Create(ControllerContext actionContext)
{
if (actionContext == null)
throw new ArgumentNullException(nameof(actionContext)); Type serviceType = actionContext.ActionDescriptor.ControllerTypeInfo.AsType(); var target = actionContext.HttpContext.RequestServices.GetRequiredService(serviceType); var properties = serviceType.GetTypeInfo().DeclaredProperties; var proxyGenerator = actionContext.HttpContext.RequestServices.GetService<ProxyGenerator>(); var proxy = proxyGenerator.CreateClassProxyWithTarget(serviceType, target); foreach (PropertyInfo info in properties)
{
//属性注入
if (info.GetCustomAttribute<AutowiredAttribute>() != null)
{
var propertyType = info.PropertyType;
object impl = null;
var qualifierAttribute = info.GetCustomAttribute<QualifierAttribute>();
if (qualifierAttribute != null)
{
var serviceName = qualifierAttribute.Name;
var implList = actionContext.HttpContext.RequestServices.GetServices(propertyType);
foreach (var tmpImpl in implList)
{
if (ProxyUtil.IsProxy(tmpImpl))
{
var addition = tmpImpl as IDependencyAddition;
if (addition?.Name == serviceName)
{
impl = tmpImpl;
break; ;
}
}
}
}
else
{
impl = actionContext.HttpContext.RequestServices.GetService(propertyType);
} if (impl != null)
{
info.SetValue(proxy, impl);
}
} //配置值注入
if (info.GetCustomAttribute<ValueAttribute>() is ValueAttribute valueAttribute)
{
var value = valueAttribute.Value;
if (actionContext.HttpContext.RequestServices.GetService(typeof(IConfiguration)) is IConfiguration configService)
{
var pathValue = configService.GetSection(value).Value;
if (pathValue != null)
{
var pathV = Convert.ChangeType(pathValue, info.PropertyType);
info.SetValue(proxy, pathV);
}
} }
} return proxy;
} /// <inheritdoc />
public virtual void Release(ControllerContext context, object controller)
{
}
}
修改Startup.cs类里的ConfigureServices方法,可以看到,我们在注册WheelA和WheelB的时候,为服务添加了名称,并且随手添加了个拦截器,代码如下:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
}); services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSB(); services.AddSingleton<ProxyGenerator>(); services.AddSbScoped<Engine>(typeof(TransactionalInterceptor)); services.AddSbScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped(typeof(TransactionalInterceptor)); services.AddSbScoped<ICar, Car>(typeof(TransactionalInterceptor)); services.AddSbScoped<IDbFactory, DbFactory>();
services.AddSbRepositoryService(typeof(TransactionalInterceptor));
services.AddSbScoped<IAddOilService, AddOilService>(typeof(TransactionalInterceptor)); services.AddSbScoped<IWheel, WheelA>("A轮胎", typeof(TransactionalInterceptor));
services.AddSbScoped<IWheel, WheelB>("B轮胎", typeof(TransactionalInterceptor));
}
控制器类HomeController,代码如下:
public class HomeController : Controller
{
[Autowired]
public ICar Car { set; get; } [Autowired]
public IDistributedCache Cache { set; get; } [Value("description")]
public string Description { set; get; } [Autowired]
public IAddOilService AddOilService { set; get; } public IActionResult Index()
{ var car = Car; AddOilService.AddOil(); Car.Fire(); Console.WriteLine(Description); return View();
}
}
核心原理,从SummerBootExtentions类完整源码里单独抽取mixin服务注册和属性注入的代码,可以看到在生成动态代理的时候,在option里添加了mixin实例,也就是我们自定义的DependencyAddition,里面包含了这个服务的名称,在注入时,判断是否有[QualifierAttribute]注解,如果有,就获取整个接口的实现类列表,在循环里,判断是否为动态代理类,如果是,就把每个类实例强转为附加类接口IDependencyAddition,然后获取名称,如果名称和我们[QualifierAttribute]注解指定的名称相同,则注入。
var proxyGenerator = provider.GetService<ProxyGenerator>();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new DependencyAddition() { Name = name });
var proxy = proxyGenerator.CreateInterfaceProxyWithTarget(serviceType, target, options, interceptors.ToArray());
//属性注入
if (info.GetCustomAttribute<AutowiredAttribute>() != null)
{
var propertyType = info.PropertyType;
object impl = null;
var qualifierAttribute = info.GetCustomAttribute<QualifierAttribute>();
if (qualifierAttribute != null)
{
var serviceName = qualifierAttribute.Name;
var implList = provider.GetServices(propertyType);
foreach (var tmpImpl in implList)
{
if (ProxyUtil.IsProxy(tmpImpl))
{
var addition = tmpImpl as IDependencyAddition;
if (addition?.Name == serviceName)
{
impl = tmpImpl;
break; ;
}
}
}
}
else
{
impl = provider.GetService(propertyType);
} if (impl != null)
{
info.SetValue(target, impl);
}
}
2.效果图
先注入A轮胎
从上图可以看到,生成的动态代理类里包含了Name这个属性,并且名称为A轮胎。
控制台输出,也显示了“我是A轮胎,我正在滚”,符合预期。
注入B轮胎
从上图可以看到,生成的动态代理类里包含了Name这个属性,并且名称为B轮胎。
控制台输出,也显示了“我是B轮胎,我正在滚”,符合预期。
3. 写在最后
通过[Autowired]注解和[Qualifier("B轮胎")]注解联合使用,我们就实现了一个接口多个实现类的精准属性注入,代码看起来是不是舒服多了呢~,我始终相信,如果一件事我讲的别人听不明白,那一定是我讲的不够有趣,不够通俗易懂,所以大白话是必须的,如果文章还有哪里说得太深奥,欢迎给我指出来,同时net core里还有啥功能让你觉得用起来特别别扭的,请!一定!告诉我!,i will try it,因为这特别有趣,因为我一直践行的编程理念就是万物靠注入,万物可拦截,万物皆注解,始终相信IOC和AOP是构建软件的基石,哈哈哈哈哈。
如果这篇文章对你有所帮助,不妨点个赞咯。
net core天马行空系列: 一个接口多个实现类,利用mixin技术通过自定义服务名,实现精准属性注入的更多相关文章
- net core天马行空系列:SummerBoot,将SpringBoot的先进理念与C#的简洁优雅合二为一
系列目录 1.net core天马行空系列:原生DI+AOP实现spring boot注解式编程 2.net core天马行空系列: 泛型仓储和声明式事物实现最优雅的crud操作 3.net core ...
- net core天马行空系列:移植Feign,结合Polly,实现回退,熔断,重试,超时,做最好用的声明式http服务调用端
系列目录 1.net core天马行空系列:原生DI+AOP实现spring boot注解式编程 2.net core天马行空系列: 泛型仓储和声明式事物实现最优雅的crud操作 3.net core ...
- net core天马行空系列: 泛型仓储和声明式事物实现最优雅的crud操作
系列目录 1.net core天马行空系列:原生DI+AOP实现spring boot注解式编程 哈哈哈哈,大家好,我就是那个高产似母猪的三合,长久以来,我一直在思考,如何才能实现高效而简洁的仓储模式 ...
- ExpandoObject与DynamicObject的使用 RabbitMQ与.net core(一)安装 RabbitMQ与.net core(二)Producer与Exchange ASP.NET Core 2.1 : 十五.图解路由(2.1 or earler) .NET Core中的一个接口多种实现的依赖注入与动态选择看这篇就够了
ExpandoObject与DynamicObject的使用 using ImpromptuInterface; using System; using System.Dynamic; names ...
- DevOps运动的缘起 将DevOps想象为一种编程语言里面的一个接口,而SRE类实现了这个接口
SRE vs DevOps:是敌是友? - DockOne.io http://www.dockone.io/article/5935 RE vs DevOps:是敌是友? [编者的话]网站可靠 ...
- net core天马行空系列:降低net core门槛,数据库操作和http访问仅需写接口,实现类由框架动态生成
引文 hi,大家好,我是三合.不知各位有没有想过,如果能把数据库操作和http访问都统一封装成接口(interface)的形式, 然后接口对应的实现类由框架去自动生成,那么必然能大大降低工作量,因 ...
- net core天马行空系列-可用于依赖注入的,数据库表和c#实体类互相转换的接口实现
1.前言 hi,大家好,我是三合.作为一名程序猿,日常开发中,我们在接到需求以后,一般都会先构思一个模型,然后根据模型写实体类,写完实体类后在数据库里建表,接着进行增删改查, 也有第二种情况,就是有些 ...
- 在.NET Core中处理一个接口多个不同实现的依赖注入问题
前言 近段时间在准备公司的技术分享,所以这段时间将大部分时间放在准备分享内容上去了.博客也就停了一下下. 在.NET Core中处理依赖注入问题时,往往是定义好了一个操作规范的接口,会有N多个基于不同 ...
- .NET Core中的一个接口多种实现的依赖注入与动态选择看这篇就够了
最近有个需求就是一个抽象仓储层接口方法需要SqlServer以及Oracle两种实现方式,为了灵活我在依赖注入的时候把这两种实现都给注入进了依赖注入容器中,但是在服务调用的时候总是获取到最后注入的那个 ...
随机推荐
- S2:.net
1.net框架结构 主要包含公共语言运行时(CLR)和框架类库(.NET Framework 类库 ,FCL) 2.CLR 1.对于一个将要面向.NET平台进行开发的人来说,了解一下.NET平台的整 ...
- 深入Apache NiFi 之源码学习
前言 要问 Hortonworks 这家公司最有产品力的产品是什么,我觉得是 Apache NiFi.去年Cloudera 和 Hortonworks 合并之后,以 Cloudera 为主,两家公司进 ...
- js 双向绑定数据
let aaa = []; let bbb = [1,2,3]; let ccc = [0,9,8]; aaa = bbb; //此时aaa与bbb被绑定(aaa指向bbb的指向) ,若使用push则 ...
- .net core使用MQTT
废话不多说,我们来直接实践…… 一.搭建mqtt控制台服务端 新建一个.net core控制台项目,然后使用Nuget添加MQTTnet包,我这里使用2.4版本,注意不同版本,代码写法不相同,如下图 ...
- Unity的弱联网Json数据传输
注意事项: 关于dictionary转json的工程中遇到一点问题:要手动添加双引号. 关于json转dictionary:同样需要手动去掉双引号,否则添加到dictionary中的字符串会带有双引号 ...
- Activiti6系列(4)- 三个war包的数据源及密码修改
一.activiti-app修改数据源和密码 1.使用sublimetext工具打开tomcat,方便进行配置文件的修改. 找到被解压的war包,activiti-app/WEB-INF/classe ...
- Java 8原生API也可以开发响应式代码?
前段时间工作上比较忙,这篇文章一直没来得及写,本文是阅读<Java8实战>的时候,了解到Java 8里已经提供了一个异步非阻塞的接口(CompletableFuture),可以实现简单的响 ...
- 使用Jasypt对SpringBoot配置文件加密
# **前言** 在日前安全形势越来越严重的情况下,让我意识到在项目中存在一个我们经常忽略的漏洞,那就是我们的项目的配置文件中配置信息的安全,尤其是数据库连接的用户名和密码的安全.所以这里我们就需要对 ...
- Linux安装nfs共享文件
简介nfs nfs网络文件系统常用于共享音视频,图片等静态资源.将需要共享的资源放到NFS里的共享目录,通过服务器挂载实现访问. 服务端安装: yum install -y nfs-utils rpc ...
- C#高级语法之泛型、泛型约束,类型安全、逆变和协变(思想原理)
一.为什么使用泛型? 泛型其实就是一个不确定的类型,可以用在类和方法上,泛型在声明期间没有明确的定义类型,编译完成之后会生成一个占位符,只有在调用者调用时,传入指定的类型,才会用确切的类型将占位符替换 ...