Autofac Getting Started(默认的构造函数注入)
https://autofaccn.readthedocs.io/en/latest/getting-started/index.html
The basic pattern for integrating Autofac into your application is:
- Structure your app with inversion of control (IoC) in mind.
- Add Autofac references.
- At application startup…
- Create a ContainerBuilder.
- Register components.
- Build the container and store it for later use.
- During application execution…
- Create a lifetime scope from the container.
- Use the lifetime scope to resolve instances of the components.
This getting started guide walks you through these steps for a simple console application. Once you have the basics down, you can check out the rest of the wiki for more advanced usage and integration information for WCF, ASP.NET, and other application types.
class Program
{
private static IContainer Container { get; set; } static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterType<ConsoleOutput>().As<IOutput>();
builder.RegisterType<TodayWriter>().As<IDateWriter>();
Container = builder.Build(); // The WriteDate method is where we'll make use
// of our dependency injection. We'll define that
// in a bit.
WriteDate(); Console.ReadLine();
} public static void WriteDate()
{
// Create the scope, resolve your IDateWriter,
// use it, then dispose of the scope.
using (var scope = Container.BeginLifetimeScope())
{
var writer = scope.Resolve<IDateWriter>();
writer.WriteDate();
}
}
}
- The “WriteDate” method asks Autofac for an
IDateWriter. - Autofac sees that
IDateWritermaps toTodayWriterso starts creating aTodayWriter. 手动resolve - Autofac sees that the
TodayWriterneeds anIOutputin its constructor. - Autofac sees that
IOutputmaps toConsoleOutputso creates a newConsoleOutputinstance. 自动resolve(构造函数注入) - Autofac uses the new
ConsoleOutputinstance to finish constructing theTodayWriter. - Autofac returns the fully-constructed
TodayWriterfor “WriteDate” to consume.
Note: generally speaking, service location is largely considered an anti-pattern (see article). That is, manually creating scopes everywhere and sprinkling use of the container through your code is not necessarily the best way to go. Using the Autofac integration libraries you usually won’t have to do what we did in the sample app above. Instead, things get resolved from a central, “top level” location in the application and manual resolution is rare. Of course, how you design your app is up to you.
Autofac Getting Started(默认的构造函数注入)的更多相关文章
- MVC Autofac构造函数注入
建立 空的 MVC4项目 首先引用 NuGet 里 autofac 和 autofac .integration. mvc 然后 建立Model public class Person { publi ...
- Autofac 的构造函数注入方式
介绍 该篇文章通过一个简单的 ASP.NET MVC 项目进行介绍如何使用 autofac 及 autofac 的 MVC 模块进行依赖注入.注入方式通过构造函数. 在编写 aufofac 的依赖注入 ...
- .net core番外第2篇:Autofac的3种依赖注入方式(构造函数注入、属性注入和方法注入),以及在过滤器里面实现依赖注入
本篇文章接前一篇,建议可以先看前篇文章,再看本文,会有更好的效果. 前一篇跳转链接:https://www.cnblogs.com/weskynet/p/15046999.html 正文: Autof ...
- AutoFac (控制反转IOC 与依赖注入DI)
重要的参考资料http://www.cnblogs.com/xdp-gacl/p/4249939.html 谈谈对Spring IOC的理解 IOC概念(很重要) 项目 先引入AutoFac 和Aut ...
- spring构造函数注入、setter方法注入和接口注入
Spring开发指南中所说的三种注入方式: Type1 接口注入 我们常常借助接口来将调用者与实现者分离.如: public class ClassA { private InterfaceB clz ...
- MVC3+AutoFac实现程序集级别的依赖注入
1.介绍 所谓程序集级别的依赖注入是指接口和实现的依赖不使用配置文件或硬代码实现(builder.RegisterType<UserInfoService>().As<IU ...
- spring依赖注入之构造函数注入,set方法注入
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- ASP.NET Core 依赖注入(构造函数注入,属性注入等)
原文:ASP.NET Core 依赖注入(构造函数注入,属性注入等) 如果你不熟悉ASP.NET Core依赖注入,先阅读文章: 在ASP.NET Core中使用依赖注入 构造函数注入 构造函数注 ...
- AutoFac实现程序集级别的依赖注入
1.介绍 所谓程序集级别的依赖注入是指接口和实现的依赖不使用配置文件或硬代码实现(builder.RegisterType<UserInfoService>().As<IU ...
随机推荐
- cocos2d安卓自动编译脚本去掉复制Resources资源和签名功能
去掉这两个功能的原因: 1.因为有时候打包是分渠道的,不同的渠道资源也可能不一样,所以不能直接复制资源. 2.如果用cocostudio打release包,因为要输入签名地址,会导致在自动签名处停住不 ...
- 亲测Google开源JPEG图片压缩算法,可将JPEG文件缩小%35
具体方法如下(windows 平台) GitHub上下载guetzli源码,地址https://github.com/google/guetzli GitHub上下载 vcpkg源码,地址https: ...
- Linux ping不通百度的解决方法
今天在学习DNS的时候遇到了一个问题,我的虚拟机能够ping通ip地址,却ping不通www.baidu.com www.qq.com等域名,先是出现了以下报错: 折腾了几个小时终于找到解决办法 1. ...
- 【IDEA】安装Jrebel插件:JRebel6.4.3+破解补丁
Jrebel 通过社交分享得到的激活码不能用了.在网上找了一波,发现通过反向代理破解最好,但激活过程中报错 Check your network connection and/or VPN setti ...
- PIP源使用国内镜像
对于Python开发用户来讲,PIP安装软件包是家常便饭.但国外的源下载速度实在太慢,浪费时间.而且经常出现下载后安装出错问题.所以把PIP安装源替换成国内镜像,可以大幅提升下载速度,还可以提高安装成 ...
- Linux网络配置:设置IP地址、网关DNS、主机名
查看网络信息 1.ifconfig eth0 2.ifconfig -a 3.ip add 设置主机名需改配置文件: /etc/hosts /etc/sysconfig/network vim /et ...
- Powershell计算时间间隔(New-TimeSpan)
在Windows PowerShell里New-TimeSpan cmdlet提供了一种方法做日期算法. 计算时间间隔: 这个命令告诉你今天的日期与2006年除夕之间的天数: New-TimeSpan ...
- 【Python之路】第二十二篇--Django【基础篇】
1 Django流程介绍 MTV模式 著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. 模型负责业 ...
- django 表单系统 之 forms.Form
继承forms.Form实现django表单系统 参考: https://www.cnblogs.com/zongfa/p/7709639.html https://www.cnblogs.com/c ...
- 手势识别:GestureDetector
当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener接口,通过重写他的onTouch(View v, ...