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 ...
随机推荐
- ssh框架搭建出现的问题和解决
[说明]今天尝试从头开始搭建ssh 框架, 真心是有点不太容易,可能是第一次吧,之前都是小打小闹. 一:今日完成 搭建 Spring 环境 --> 然后搭建 Hibernate 环境 --> ...
- git 拉取远程分支到本地并建立关联关系
git拉取远程分支到本地 一.查看远程分支 使用如下git命令查看所有远程分支: git branch -r 二.拉取远程分支并创建本地分支 方法一 使用如下命令: git checkout ...
- 学习使用turtlebot2——安装ROS Indigo系统
最近在学习使用turtlebot2,特此做一些学习记录. 安装ROS前要先决定自己电脑的Ubuntu(乌班图)系统.现在学习ROS常使用的Ubuntu系统有Ubuntu 16.04 和Ubuntu14 ...
- scrapy工程创建及pycharm运行
1.通过命令行创建scrapy工程项目 scrapy startproject (工程名) scrapy startproject myxml 2.利用爬虫模板设置爬虫文件 在这个过程中我们可以先利用 ...
- npm的本地模式与全局模式
关于npm的本地模式和全局模式的总结: npm install命令就是采用本地模式,即把包安装到当前目录的node_modules子目录下:npm [install/i] [package_name] ...
- Oracle database精装版11gR2入门详细连接教程
对于11g本身比较简单,适合学习者使用,对电脑要求相对较低. 自己一个人单机学习使用. 工具/原料 Oracle Database Express Edition 11g Release 2安装包 ...
- Oracle数据库模型(OLAP/OLTP)
数据库模型 选择数据库模型: 联机事务处理OLTP(on-line transaction processing) OLTP是传统的关系数据库的主要应用,基本的.日常的事务处理.例如银行交易. OLT ...
- Exchange Version and UpdateRollups
Exchange Server 2010 Product name Build number Date KB Microsoft Exchange Server 2010 RTM 14.0.639.2 ...
- PHP的几种排序方法
<?php /** * PHP最常用的四个排序方法及二种查找方法 * 下面的排序方法全部都通过测试 * auther : soulence * date : 2015/06/20 */ //PH ...
- SQL竖表转换成横表统计
#创建表user_score create table user_score ( name varchar(20), subjects varchar(20), score int ); insert ...