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 ...
随机推荐
- 认识tornado(五)
Tornado 自带了模板系统,模板语法与 Django 差异不大.这里简单地介绍如何使用 Tornado 的模板系统. 首先是编写 URL 规则与 Handler: 01 class NowaMag ...
- VS 忽略文件 Git 向远程添加问题
背景介绍: 我团队有个叫李恕华的技术大牛,最近在研究redis 然后向一个叫做MTS的项目中引入了redis,感觉很吊 问题: vs2017+git管理代码 其他人分支里一直提示找不到redis文件 ...
- 【BZOJ4665】小w的喜糖 容斥+组合数
[BZOJ4665]小w的喜糖 Description 废话不多说,反正小w要发喜糖啦!! 小w一共买了n块喜糖,发给了n个人,每个喜糖有一个种类.这时,小w突发奇想,如果这n个人相互交换手中的糖,那 ...
- Oracle Instant Client的安装和使用
转自:https://www.cnblogs.com/chinalantian/archive/2011/09/09/2172145.html 根据自己需求到Oracle网站(http://www.o ...
- Leetcode-Convert Sorted List to BST.
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 『浅入浅出』MySQL 和 InnoDB
作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.PostgreSQL,但是一直以来也没有对数据库有一个非常清晰并且成体系 ...
- delphi xe -芒果数据库(FDConnection,DataSource,FDMongoQuery,FDMongoDataSet)连接,查询(展示数据),这里有mongodb为例子
一.连接 1.FDConnection:创建一个临时连接定义 资料:http://www.cnblogs.com/zhenfei/p/4105515.html 连接芒果数据库:选则Mongo(芒果), ...
- Javascript调用WinForm方法
window.external.MyMessageBox('javascript访问C#代码')
- Python菜鸟之路:Django 序列化数据
类型一:对于表单数据进行序列化 这时需要用到ErrorDict. ret['errors'] = obj.errors.as_data() result = json.dumps(ret, cls=J ...
- Redis2.8配置文件详解(转)
add by zhj : 没找到本文的原文.另外,redis配置文件中文翻译 也翻译的不错,可以与本文对照看.两篇文章都是以Redis2.8来介绍的 在Redis中直接启动redis-server服务 ...