.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 ...
随机推荐
- 合并数组,改变原数组apply与不改变原数组
一看见合并数组,可能第一反应就是concat,concat确实具有我们想要的行为,但它实际上并不附加到现有数组,而是创建并返回一个新数组. 同样你也许会想到ES6的扩展运算符... 但 ...
- MySql/Oracle树形结构查询
Oracle树形结构递归查询 在Oracle中,对于树形查询可以使用start with ... connect by select * from treeTable start with id='1 ...
- python入门(五):切片列表元祖字典
1.切片 针对序列,使用切片可以获得我们想要的内容 序列:字符串.列表.元祖 特点:可以使用坐标获取某一个值.坐标是从0开始算 >>> s="0123456789" ...
- netty(六) websocket开发应用
package com.lance.net.server.common; import java.net.InetSocketAddress; import org.springframework.s ...
- GitLab代码行数统计--统计增加与删除行数
#!/bin/bashmaster_dev='master'date_star='2018-11-01'date_end='2018-11-30'path1=`find /home/gitlab_da ...
- Polynomial regression
- Retrofit 2.0基于OKHttp更高效更快的网络框架 以及自定义转换器
时间关系,本文就 Retrofit 2.0的简单使用 做讲解 至于原理以后有空再去分析 项目全面.简单.易懂 地址: 关于Retrofit 2.0的简单使用如下: https://gitee.c ...
- 微信H5开发
1,line-height 不垂直居中 解决方法:用padding写高度 2,手指滑动事件 touchmove 3,页面滚动事件:touchmove 4,要写不同手机适配的基数font-siz ...
- spring mvc+mybatis 构建 cms + 实现UC浏览器文章功能
最近公司在模拟UC浏览器做一个简单的cms系统,主要针对于企业内部的文章浏览需求,这边考虑用户大多用mobile浏览文章内容,故使用原生的ios和android进行开发,后面也会集成html5. 1. ...
- poj3304(是否存在一条直线与所有给出线段相交
题意:给出n条线段,问你是否存在一条直线让他与所有线段相交. 思路:枚举两条直线的起点和终点做一条直线,看他是否与所有线段相交. #include<cstdio> #include< ...