Autofac property injection
https://autofaccn.readthedocs.io/en/latest/register/prop-method-injection.html
Property and Method Injection
While constructor parameter injection is the preferred method of passing values to a component being constructed, you can also use property or method injection to provide values.
Property injection uses writeable properties rather than constructor parameters to perform injection.
Method injection sets dependencies by calling a method.
Property Injection
If the component is a lambda expression component, use an object initializer:
builder.Register(c => new A { B = c.Resolve<B>() }); B作为A的属性
To support circular dependencies, use an activated event handler:
builder.Register(c => new A()).OnActivated(e => e.Instance.B = e.Context.Resolve<B>());
If the component is a reflection component 构造函数注入的, use the PropertiesAutowired() modifier to inject properties:
builder.RegisterType<A>().PropertiesAutowired();
If you have one specific property and value to wire up, you can use the WithProperty() modifier:
builder.RegisterType<A>().WithProperty("PropertyName", propertyValue);
Method Injection
The simplest way to call a method to set a value on a component is to use a lambda expression component and handle the method call right in the activator:
builder.Register(c => {
var result = new MyObjectType();
var dep = c.Resolve<TheDependency>();
result.SetTheDependency(dep);
return result;
});
If you can’t use a registration lambda, you can add an activating event handler:
builder
.Register<MyObjectType>()
.OnActivating(e => {
var dep = e.Context.Resolve<TheDependency>();
e.Instance.SetTheDependency(dep);
});
扩展阅读
https://www.cnblogs.com/jiagoushi/p/4084145.html
http://www.cnblogs.com/artech/p/asp-net-core-di-di.html
Autofac property injection的更多相关文章
- Autofac Property Injection and Method Injection
While constructor parameter injection is the preferred method of passing values to a component being ...
- Property Injection in Asp.Net Core (转载)
问: I am trying to port an asp.net application to asp.net core. I have property injection (using ninj ...
- .NET手记-Autofac进阶(属性和方法注入 Property and Method Injection)
尽管构造函数参数注入是传递参数值给当前构造的组件的优先方式,但是你也可以使用属性或者方法注入来提供参数值. 属性注入使用可写入的变量而不是构造函数参数来完成注入.方法注入则通过方法来设置依赖项. 属性 ...
- ASP.NET MVC 5 使用autofac实现DI
使用Nuget添加Autofac.MVC的引用 启动项设置 注册Controller 注册ModelBinder 注册相关的web abstraction 为View层启用属性注入 为Action F ...
- autofac 使用
var builder = new ContainerBuilder();var container = builder.Build(); var assemblies = new Directory ...
- 依赖注入容器Autofac与MVC集成
Autofac是应用于.Net平台的依赖注入(DI,Dependency Injection)容器,具有贴近.契合C#语言的特点.随着应用系统的日益庞大与复杂,使用Autofac容器来管理组件之间的关 ...
- .NET手记-为ASP.NET MVC程序集成Autofac
MVC Autofac总是会紧跟最新版本的ASP.NET MVC框架,所以文档也会一直保持更新.一般来讲,不同版本的框架集成Autofac的方法一般不变. MVC集成需要引用 Autofac.Mvc5 ...
- 如何创建一个Quartz.NET的工作,需要注射autofac
问题: 使用 Quartz.Net 做定时任务时,实现IJob对象的服务,Autofac不会自动注入,使用构造函数会直接出现异常,无法执行Execute方法. 解决方式 方法一: 使用 Autofac ...
- Autofac log4net Integration Module
log4net Integration Module While there is no specific assembly for log4net support, you can easily i ...
随机推荐
- 面试题思考:Stack和Heap的区别
堆栈的概念: 堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈是个特殊的存储区,主要功能是暂时存放数据和地址,通常 ...
- [Go语言]从Docker源码学习Go——结构和函数的定义
Docker在最近很火,而作为Docker的开发语言-Go也再次被大家提到. 已经使用Docker一段时间了,但是对于源码,尤其是其开发语言Go却一直是一知半解. 最近准备利用空余时间从Docker源 ...
- cookie的简单留言框
我们在网页浏览时退出后,再次进入时会有上次的记录,这就用的上cookie属性了,cookie就是服务器暂存放在你计算机上的一笔资料,好让服务器用来辨认你的计算机.当你在浏览网站的时候,Web服务器会先 ...
- FineReport---函数
1.NUMTO()需要将数字2345转换成二三四五:NUMTO(2345) 2.Toimage函数:Toimage(path)用于在报表中显示某一路径path下的图片 3.row():为获取当前行号 ...
- <2014 04 16> 上班实习第一天
找了家开发3D printer的创业公司实习,做(嵌入式)软件工程师.今天第一天. 1.熟悉了基于SLA技术的3D打印机的主要关键问题,机械结构. 控制系统是基于PC-Clinet和一个树莓派ARM/ ...
- 利用epoll实现异步IO
之前异步IO一直没搞明白,大致的理解就是在一个大的循环中,有两部分:第一部分是监听事件:第二部分是处理事件(通过添加回调函数的方式).就拿网络通信来说,可以先通过调用 select 模块中的 sele ...
- jQuery选择器-常用
jQuery选择器-常用 1.基本选择器 $("#id") //ID选择器 $("div") //元素选择器 $(".classname") ...
- mysql乐观锁总结和实践(转)
原文:mysql乐观锁总结和实践 上一篇文章<MySQL悲观锁总结和实践>谈到了MySQL悲观锁,但是悲观锁并不是适用于任何场景,它也有它存在的一些不足,因为悲观锁大多数情况下依靠数据库的 ...
- Redis、MongoDB及Memcached的区别 Redis(内存数据库)
Redis.MongoDB及Memcached的区别 Redis(内存数据库) 是一个key-value存储系统(布式内缓存,高性能的key-value数据库).和Memcached类似,它支持存储的 ...
- redis中默认有多少个哈希槽?
Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余 ...