Autofac Property Injection 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>() });
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);
});
Autofac Property Injection and Method Injection的更多相关文章
- .NET手记-Autofac进阶(属性和方法注入 Property and Method Injection)
尽管构造函数参数注入是传递参数值给当前构造的组件的优先方式,但是你也可以使用属性或者方法注入来提供参数值. 属性注入使用可写入的变量而不是构造函数参数来完成注入.方法注入则通过方法来设置依赖项. 属性 ...
- Autofac property injection
https://autofaccn.readthedocs.io/en/latest/register/prop-method-injection.html Property and Method I ...
- Spring之Method Injection
对于Spring的多数用户而言,主要的Bean存在形式都是单例,当一个单例需要结合另一个单例协作或者一个非单例与另一个非单例协作时,典型的做法是通过属性的形式注入,但是当两个Bean的声明周期不同时候 ...
- Spring Setter Injection and Constructor Injection
Setter Injection AppContext.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- Windows Dll Injection、Process Injection、API Hook、DLL后门/恶意程序入侵技术
catalogue 1. 引言2. 使用注册表注入DLL3. 使用Windows挂钩来注入DLL4. 使用远程线程来注入DLL5. 使用木马DLL来注入DLL6. 把DLL作为调试器来注入7. 使用c ...
- bWAPP练习--injection篇SQL Injection (GET/Search)
SQL注入: SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具体来说,它是利用现有应用程序,将(恶意)的SQL命令注入到 ...
- 译:Spring框架参考文档之IoC容器(未完成)
6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...
- Spring框架文档与API(4.3.6版本)
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ Table of Contents I ...
- Inversion of Control Containers and the Dependency Injection pattern
https://martinfowler.com/articles/injection.html One of the entertaining things about the enterprise ...
随机推荐
- Splay树学习
首先给出一论文讲的很好: http://www.docin.com/p-63165342.html http://www.docin.com/p-62465596.html 然后给出模板胡浩大神的模板 ...
- Qt5_vs2013_error_C2001: 常量中有换行符__ZC
ZC: 这里是解决 Windows平台下的这个 编译error :“error C2001: 常量中有换行符”. ZC: 我现在(20161221)的处理方式:vs2010或vs2015 将cpp文件 ...
- 使用ARouter遇到的坑
跨模块跳转不能跳转 需要被跳转的模块或者说使用了ARouter注解的模块都要加上这个 dependencies{ annotationProcessor rootProject.ext.arou ...
- [sql]join的5种方式:inner join、left(outer) join、right (outer) Join、full(outer) join、cross join
现在有两张表 如下图所示: 一 .inner join 返回的结果:两个表的交集行 二. left join 是left outer join的简写 返回结果:左表的 ...
- 递归--练习10--noi1696逆波兰表达式
递归--练习10--noi1696逆波兰表达式 一.心得 递归大法好 二.题目 1696:逆波兰表达式 总时间限制: 1000ms 内存限制: 65536kB 描述 逆波兰表达式是一种把运算符前置 ...
- Rails 5 Test Prescriptions 最后一章,如何测试继承下来的代码,legacy code
Set expectations 你不可能把一个老旧的代码野兽只用一晚就转变成优雅的奇迹marvel.你需要如下做法: 让自己有好的状态,用15分钟挥舞拳头诅咒之前的程序员 开始工作,这个codeba ...
- HDU 5816 状压DP&排列组合
---恢复内容开始--- Hearthstone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java ...
- Leetcode 35
//在数组最后插入INT_MAX是个好方法class Solution { public: int searchInsert(vector<int>& nums, int targ ...
- 面筋: 奇虎360 c++ 后台开发 实习生 面试
投的是360上海的商业化部门,岗位是C++服务端开发实习生,记录一下面试历程: 视频面试,但是是有代码框让你写代码的. 一面: Q:先说一下个人信息,做过的项目 A:.......... Q:先写个翻 ...
- specialized English for automation-Lesson 3 CMOS Logic Circuit
CMOS logic is a newer technology, based on the use of complementary MOS transistors toperform logic ...