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 ...
随机推荐
- xcode 运行 lua版本崩溃 解决方案
问题描述:运行到LuaStack::init() 崩溃 原因: luajit不支持arm64 解决方案:编译luajit64位静态库 a.可以直接下载别人编译好的库,然后直接覆盖cocos2d\ext ...
- fis3 部署手册
为什么使用FIS3 项目上线一段时间后如果更新JS或CSS文件,而客户端已经对该文件缓存过了,那就有可能会无法及时更新而继续采用旧的JS或CSS文件,无法达到想要的效果. 处理类似情况最有效的解决方案 ...
- java编程思想 英文版 打卡
计划 2017.3.1 购入 准备花一个月的时间阅读完, 共1500页,最后两章是GUI的内容,只需要到1300页就行了 目的有三: 熟悉java基础 提升英语阅读能力,好久没读英文书籍了 补补oop ...
- Oracle Schema Objects——Tables——Table Compression
Oracle Schema Objects Table Compression 表压缩 The database can use table compression to reduce the amo ...
- 数据库系统概述(Data Model、DBMS、DBS、RDBS、Structured Query Language)
数据Data 描述事物的符号记录成为数据. 数据是数据库中存储的基本对象. 除了基本的数字之外.像图书的名称.价格.作者都可以称为数据. 将多种数据记录列成一张表.通过数据表管理数据. 每一行的数 ...
- JS给html控件赋值
<html> <head> <title> JS给html控件赋值 </title> <script language="javascr ...
- flink hadoop yarn
新一代大数据处理引擎 Apache Flink https://www.ibm.com/developerworks/cn/opensource/os-cn-apache-flink/ 新一代大数据处 ...
- Windows服务的调试
1.服务为其他程序调用的情况:首先停止服务,在项目中设置断点,重新启动服务,点击项目中工具,附加到进程,运行调用服务的程序,即可进入之前设置的断点,进而进行调试. 2.服务内方法为自动执行的情况:首先 ...
- pytho创建二维码简单版
pytho创建二维码简单版 import qrcode aa = qrcode.make("https://github.com/phygerr/") aa.save('C:\Us ...
- time 模块,random模块,os模块
一 :time 模块 python中,通常有几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(t ...