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的更多相关文章

  1. Autofac Property Injection and Method Injection

    While constructor parameter injection is the preferred method of passing values to a component being ...

  2. 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 ...

  3. .NET手记-Autofac进阶(属性和方法注入 Property and Method Injection)

    尽管构造函数参数注入是传递参数值给当前构造的组件的优先方式,但是你也可以使用属性或者方法注入来提供参数值. 属性注入使用可写入的变量而不是构造函数参数来完成注入.方法注入则通过方法来设置依赖项. 属性 ...

  4. ASP.NET MVC 5 使用autofac实现DI

    使用Nuget添加Autofac.MVC的引用 启动项设置 注册Controller 注册ModelBinder 注册相关的web abstraction 为View层启用属性注入 为Action F ...

  5. autofac 使用

    var builder = new ContainerBuilder();var container = builder.Build(); var assemblies = new Directory ...

  6. 依赖注入容器Autofac与MVC集成

    Autofac是应用于.Net平台的依赖注入(DI,Dependency Injection)容器,具有贴近.契合C#语言的特点.随着应用系统的日益庞大与复杂,使用Autofac容器来管理组件之间的关 ...

  7. .NET手记-为ASP.NET MVC程序集成Autofac

    MVC Autofac总是会紧跟最新版本的ASP.NET MVC框架,所以文档也会一直保持更新.一般来讲,不同版本的框架集成Autofac的方法一般不变. MVC集成需要引用 Autofac.Mvc5 ...

  8. 如何创建一个Quartz.NET的工作,需要注射autofac

    问题: 使用 Quartz.Net 做定时任务时,实现IJob对象的服务,Autofac不会自动注入,使用构造函数会直接出现异常,无法执行Execute方法. 解决方式 方法一: 使用 Autofac ...

  9. Autofac log4net Integration Module

    log4net Integration Module While there is no specific assembly for log4net support, you can easily i ...

随机推荐

  1. Android无线测试之—UiAutomator UiCollection API介绍

    UiCollection类介绍 一.UiCollection类说明 1)UiCollection类是UiObject类的子类,即UiObject类的所有方法都被UiCollection继承下来了,都可 ...

  2. python 的时间复杂度

    Python内置方法的时间复杂度 本文翻译自Python Wiki 本文基于GPL v2协议,转载请保留此协议. 本页面涵盖了Python中若干方法的时间复杂度(或者叫“大欧”,“Big O”).该时 ...

  3. PMPBOK 进度管理

    项目进度计划提供详尽的计划,说明项目如何以及何时交付项目范围中定义的产品.服务和成功,是一种用于沟通和管理相关方期望的工具,为绩效报告提供了依据. 进度计划方法:关键路径发或敏捷方法.创建项目经度模型 ...

  4. 【BZOJ2259】[Oibh]新型计算机 最短路

    [BZOJ2259][Oibh]新型计算机 Description Tim正在摆弄着他设计的“计算机”,他认为这台计算机原理很独特,因此利用它可以解决许多难题. 但是,有一个难题他却解决不了,是这台计 ...

  5. @RequestMapping中method的默认值是什么?

    @RequestMapping中method的默认值是什么? 没有默认值,如果不配置method, 则以任何请求形式 RequestMethod.GET, RequestMethod.POST, Re ...

  6. 第三课补充01——set类型 sorted类型命令操作详解,redis管道及事务

    1. set类型的命令操作: (1)sadd命令:向key指定的set集合添加成员 ##sadd命令:是设置set集合类型的数据,sadd  <key> <mumber> [& ...

  7. color depth 色彩深度 像素深度

    Screen.colorDepth - Web APIs | MDN https://developer.mozilla.org/en-US/docs/Web/API/Screen/colorDept ...

  8. python系列十六:Python3 面向对象

    #!/usr/bin/python #-*-coding:gbk-*- #Python3 面向对象 '''面向对象技术简介    类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了 ...

  9. 原生js实现ajax方法

    下面是一个比较完整的Ajax function ajax(){ var ajaxData = { type:arguments[0].type || "GET", url:argu ...

  10. 贪玩ML系列之CIFAR-10调参

    调参方法:网格调参 tf.layers.conv2d()中的padding参数 取值“same”,表示当filter移出边界时,给空位补0继续计算.该方法能够更多的保留图像边缘信息.当图片较小(如CI ...