.NET手记-Autofac入门Getting Started
内容主要翻译自官方文档,原文请看:http://autofac.readthedocs.org/en/latest/getting-started/index.html#application-startup
将Autofac集成进你的应用的基本模式:
- 在脑海中构造基于控制反转(IoC)的应用程序架构
- 添加Autofac引用.
- 在应用启动配置流程...
- 创建一个 ContainerBuilder.
- 注册组件(components).
- build定义的ContainerBuilder生成Autofac容器,并存储它以供后续使用.
- 在程序运行时...
- 从Autofac容器(container)创建生命周期域(lifetime scope).
- 使用生命周期域来解析出组件实例.
这篇指导将会使用一个控制台程序一步一步演示如何使用Autofac. 一旦你有了基础的了解,可以到以下wiki站点查看更高级的使用方法和技巧: integration information for WCF, ASP.NET, and other application types.
构建应用架构
基于IoC的想法应该是对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它,而不是把所有类绑在一起,每次都要通过依赖new来传递实例.如果你想了解更多请看:Martin Fowler has an excellent article explaining dependency injection/inversion of control.
对于我们的示例应用程序,我们将定义一个输出当前日期的类。但是,我们不希望它和控制台绑在一起,因为我们希望以后能够测试它或用在一个没有控制台程序的地方。
我们还会去尽量抽象输出日期的机制,以便于我们后续重复使用。
我们会做这样的事情:
using System; namespace DemoApp
{
// This interface helps decouple the concept of
// "writing output" from the Console class. We
// don't really "care" how the Write operation
// happens, just that we can write.
public interface IOutput
{
void Write(string content);
} // This implementation of the IOutput interface
// is actually how we write to the Console. Technically
// we could also implement IOutput to write to Debug
// or Trace... or anywhere else.
public class ConsoleOutput : IOutput
{
public void Write(string content)
{
Console.WriteLine(content);
}
} // This interface decouples the notion of writing
// a date from the actual mechanism that performs
// the writing. Like with IOutput, the process
// is abstracted behind an interface.
public interface IDateWriter
{
void WriteDate();
} // This TodayWriter is where it all comes together.
// Notice it takes a constructor parameter of type
// IOutput - that lets the writer write to anywhere
// based on the implementation. Further, it implements
// WriteDate such that today's date is written out;
// you could have one that writes in a different format
// or a different date.
public class TodayWriter : IDateWriter
{
private IOutput _output;
public TodayWriter(IOutput output)
{
this._output = output;
} public void WriteDate()
{
this._output.Write(DateTime.Today.ToShortDateString());
}
}
}
添加Autofac引用
首先添加Autofac引用到你的项目,这个例子中我们仅添加Autofac Core引用,其他类型应用程序可能会用到不同的Autofac.Integration类库。
通过NuGet可以很容易为项目引入引用,如图:

应用启动(Application Startup)
在
.NET手记-Autofac入门Getting Started的更多相关文章
- Autofac 入门
Autofac 入门文档 原文链接:http://docs.autofac.org/en/latest/getting-started/index.html 在程序中使用Autofac的基本模式是: ...
- 一、Autofac入门
想要将autofac集成到你的应用程序中需要经过如下步骤: 1.使用控制翻转(IoC)的思想架构你的应用程序: 2.添加autofac引用: 3.在应用程序入口...(At application s ...
- Autofac入门
注意:本文为原创文章,任何形式的转载.引用(包括但不限于以上形式)等,须先征得作者同意,否则一切后果自负. 简介 Autofac 是一个令人着迷的.NET IoC 容器. 它管理类之间的依赖关系.当应 ...
- [翻译] Autofac 入门文档
原文链接:http://docs.autofac.org/en/latest/getting-started/index.html 在程序中使用Autofac的基本模式是: 用控制反转(IoC)的思想 ...
- JAVA手记 JAVA入门(安装+Dos下运行)
JAVA入门特供= =,今天设置环境变量后用dos运行的时候发现出现“找不到或无法加载主类”,索性查了些资料重新看了看JAVA入门的部分. 声明:我的笔记本暂时用的是Win10系统,Windows其他 ...
- 随手记-egg入门
egg 入门 https://eggjs.org/zh-cn/intro/quickstart.html 1.建立项目目录2. npm i egg --save && npm i ...
- [转]ASP.NET MVC IOC 之AutoFac攻略
本文转自:http://www.cnblogs.com/WeiGe/p/3871451.html 一.为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用A ...
- ASP.NET MVC IOC 之AutoFac攻略
一.为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用AutoFac的貌似更为普遍,于是捯饬了两天,发现这个东东确实是个高大上的IOC容器~ Autofa ...
- 不复杂的Autofac注入
private static void SetAutofacWebAPI() { var builder = new ContainerBuilder(); #region 配置注册方法 string ...
随机推荐
- 用命令生成Webservice 对应的代理类
wsdl /language:C# /namespace:Camstar.WebPortal.WebPortlets.Shopfloor.SAP.GreatWall /out:gwSAPService ...
- idea编辑项目出现【Information:java: javacTask: 源发行版 7 需要目标发行版 1.7】
在编译项目时候出现问题: Information:java: javacTask: 源发行版 7 需要目标发行版 1.7 解决方案:按着图片操作,这几个地方设置的一样就可以了
- Java的三种代理模式(Spring动态代理对象)
Java的三种代理模式 1.代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩 ...
- swift 分组tableview 设置分区投或者尾部,隐藏默认间隔高度
1.隐藏尾部或者头部,配套使用 //注册头部id tv.register(JYWithdrawalRecordSectionView.self, forHeaderFooterViewReuseIde ...
- [leetcode]32. Longest Valid Parentheses最长合法括号子串
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- python--事务操作
#coding=utf-8 import sys import MySQLdb class TransferMoney(object): def __init__(self,conn): self.c ...
- stark组件开发之列表页面应用示例
已经解决的,自定义的扩展函数,功能.但是 不可能返回. 一个 固定的页面把! 应该是,点击那条 记录之后的编辑, 就会跳转到相应的,编辑页面.所以 这个标签的 <a href="/ ...
- JavaSE基础知识(4)—数组的应用
一.数组的特点.好处及使用步骤 1.数组的好处 特点:相当于用于保存一组元素的容器好处: 1.提高代码的简洁性和扩展性,且同时开辟多个空间,提高了效率 2.分类存储,且空间是连续的,容易查找 2.数组 ...
- message [Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property
springmvc前台字符串,后台Date类型字段.时间强转失败 数值:18年12月31日 15:43:21 解决方法,给时间字段加注释 @DateTimeFormat(pattern = " ...
- 循序渐进VBA EXCEL数据操作小实例
1 向指定单元格区域内写入数据 Sub example1() ) arr() = Array("A", "B", "C", "D& ...