加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

在.net core中实现字段和属性注入的示例代码

发布时间:2020-08-21 12:14:48 所属栏目:编程 来源:网络整理
导读:这篇文章首要先容了在.net core中实现字段和属性注入的示例代码,文中通过示例代码先容的很是具体,对各人的进修可能事变具有必然的参考进修代价,必要的伴侣们

Autowired(object service)要领的实现固然简朴,可是行使了服从底下的反射,这个美中不敷必要改造,早年可以行使艰涩难解的EMIT来编写,此刻有Expression,编写和阅读都简朴了许多几何,而且服从也不比EMIT差,以是我们行使表达式+缓存来改造。Autowired要领要做的就是从容器中取出吻合的工具,然后赋值给service要自动装配的字段和属性,据此我们先编写出委托的伪代码:

(obj,serviceProvider)=>{ ((TService)obj).aa=(TAAType)serviceProvider.GetService(aaFieldType); ((TService)obj).bb=(TBBType)serviceProvider.GetService(aaFieldType); ... }

留意伪代码中的范例转换,Expression表达式在编译成委托时长短常严酷的,全部转换都不能省。写表达式的时辰我风俗先写伪代码,我但愿各人也能养成这个风俗!有了伪代码我们可以开始改革AutowiredService类了:

/// <summary> /// 从容器装配service /// </summary> [AppService] public class AutowiredService { IServiceProvider serviceProvider; public AutowiredService(IServiceProvider serviceProvider) { this.serviceProvider = serviceProvider; } Dictionary<Type, Action<object, IServiceProvider>> autowiredActions = new Dictionary<Type, Action<object, IServiceProvider>>(); public void Autowired(object service) { Autowired(service, serviceProvider); } /// <summary> /// 装配属性和字段 /// </summary> /// <param></param> /// <param></param> public void Autowired(object service, IServiceProvider serviceProvider) { var serviceType = service.GetType(); if (autowiredActions.TryGetValue(serviceType, out Action<object, IServiceProvider> act)) { act(service, serviceProvider); } else { //参数 var objParam = Expression.Parameter(typeof(object), "obj"); var spParam = Expression.Parameter(typeof(IServiceProvider), "sp"); var obj = Expression.Convert(objParam, serviceType); var GetService = typeof(IServiceProvider).GetMethod("GetService"); List<Expression> setList = new List<Expression>(); //字段赋值 foreach (FieldInfo field in serviceType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { var autowiredAttr = field.GetCustomAttribute<AutowiredAttribute>(); if (autowiredAttr != null) { var fieldExp = Expression.Field(obj, field); var createService = Expression.Call(spParam, GetService, Expression.Constant(field.FieldType)); var setExp = Expression.Assign(fieldExp, Expression.Convert(createService, field.FieldType)); setList.Add(setExp); } } //属性赋值 foreach (PropertyInfo property in serviceType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { var autowiredAttr = property.GetCustomAttribute<AutowiredAttribute>(); if (autowiredAttr != null) { var propExp = Expression.Property(obj, property); var createService = Expression.Call(spParam, GetService, Expression.Constant(property.PropertyType)); var setExp = Expression.Assign(propExp, Expression.Convert(createService, property.PropertyType)); setList.Add(setExp); } } var bodyExp = Expression.Block(setList); var setAction = Expression.Lambda<Action<object, IServiceProvider>>(bodyExp, objParam, spParam).Compile(); autowiredActions[serviceType] = setAction; setAction(service, serviceProvider); } } }

代码一下子多了不少,不外因为我们前面的铺垫,领略起来也不难,至此自动装配字段和属性的处事已经写好了,下面看看怎样行使:

编写处事类,并添加[AppService]特征

[AppService] public class MyService { //functions }

在Setup的ConfigureServices要领中注册应用处事

public void ConfigureServices(IServiceCollection services) { services.AddMvc(); //注册应用处事 services.AddAppServices(); }

在其他类中注入行使,好比Controller中

public class HomeController : Controller { [Autowired] MyUserService myUserService; public HomeController(AutowiredService autowiredService) { autowiredService.Autowired(this); } }

HomeController的结构函数是不是简捷了很多呢!并且再有新的处事要注入,只要界说字段(属性也可以,不外字段更利便)就可以了,留意:我们界说的字段不能是只读的,由于我们要在AutowiredService中配置。我们还用上面的例子,看一下它的威力吧!

public class HomeController : Controller { [Autowired] UserService userService; [Autowired] OrderService orderService; [Autowired] MsgService msgService; [Autowired] OtherService otherService; [Autowired] OtherService2 otherService2; public HomeController(AutowiredService autowiredService) { autowiredService.Autowired(this); } }

感激您的寓目!全文已经完了,我们没有行使第三方容器,也没有对自带的容器大举修改和粉碎,只是在处事类的结构器中选择性的挪用了AutowiredService.Autowired(this)要领,为什么是选择性的呢,由于你还可以行使在结构器中注入的方法,乃至混用,统统都好,都不会错杂。

nuget安装:

PM> Install-Package Autowired.Core

git源码:

[Autowired.Core] https://gitee.com/loogn/Autowired.Core

更新:

支持多个AppServiceAttribute,

支持处事独一标识,通过Identifier指定处究竟现

到此这篇关于在.net core中实现字段和属性注入的示例代码的文章就先容到这了,更多相干.net core 字段和属性注入内容请搜刮剧本之家早年的文章或继承赏识下面的相干文章但愿各人往后多多支持剧本之家!

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读