内容主要翻译自官方文档,原文请看: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的更多相关文章

  1. Autofac 入门

    Autofac 入门文档 原文链接:http://docs.autofac.org/en/latest/getting-started/index.html 在程序中使用Autofac的基本模式是: ...

  2. 一、Autofac入门

    想要将autofac集成到你的应用程序中需要经过如下步骤: 1.使用控制翻转(IoC)的思想架构你的应用程序: 2.添加autofac引用: 3.在应用程序入口...(At application s ...

  3. Autofac入门

    注意:本文为原创文章,任何形式的转载.引用(包括但不限于以上形式)等,须先征得作者同意,否则一切后果自负. 简介 Autofac 是一个令人着迷的.NET IoC 容器. 它管理类之间的依赖关系.当应 ...

  4. [翻译] Autofac 入门文档

    原文链接:http://docs.autofac.org/en/latest/getting-started/index.html 在程序中使用Autofac的基本模式是: 用控制反转(IoC)的思想 ...

  5. JAVA手记 JAVA入门(安装+Dos下运行)

    JAVA入门特供= =,今天设置环境变量后用dos运行的时候发现出现“找不到或无法加载主类”,索性查了些资料重新看了看JAVA入门的部分. 声明:我的笔记本暂时用的是Win10系统,Windows其他 ...

  6. 随手记-egg入门

    egg 入门 https://eggjs.org/zh-cn/intro/quickstart.html 1.建立项目目录2.  npm i egg --save   && npm i ...

  7. [转]ASP.NET MVC IOC 之AutoFac攻略

    本文转自:http://www.cnblogs.com/WeiGe/p/3871451.html 一.为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用A ...

  8. ASP.NET MVC IOC 之AutoFac攻略

    一.为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用AutoFac的貌似更为普遍,于是捯饬了两天,发现这个东东确实是个高大上的IOC容器~ Autofa ...

  9. 不复杂的Autofac注入

    private static void SetAutofacWebAPI() { var builder = new ContainerBuilder(); #region 配置注册方法 string ...

随机推荐

  1. linux查询公网ip

    查询公网ip[出口IP] 1.curl icanhazip.com 2.curl ipecho.net/plain 3.curl www.trackip.net/i

  2. FortiGate高校图书馆SSLvpn配置案例

    1.组网及需求 某高校有一台FGT系列防火墙放置于互联网出口,拓扑如下图: 现需求通过组建sslvpn web代理模式和隧道模式以实现: 1.web代理模式:能访问 http://lib.xxxx.e ...

  3. 【开发工具】secureCRT的使用

    总结自己平时使用secureCRT中遇到的问题 1.安装 2.secureCRT设置标签显示远程主机ip地址

  4. 图片识别文字, OCR

    文章引用自: https://www.cnblogs.com/stone_w/archive/2011/10/08/2202397.html 方式一.Asprise-OCR的使用. Asprise-O ...

  5. webapi postman 415 错误

    https://blog.csdn.net/Intangible_moon/article/details/80183121 猜测:如果后台接口使用的是[fromBody]标签的话,需要使用raw方式

  6. 七、eclipse添加离线约束,使不联网也能有一些代码的提示,例如dubbo

    eclipse添加离线约束,使不联网也能有一些代码的提示,例如dubbo 1.将dubbo.xsd文件放到一个无中文目录下 2.eclipse->windows->preferences- ...

  7. 深入理解JVM(一)编译openJDK

    此文总结的很不错:https://www.cnblogs.com/ACFLOOD/p/5528035.html 准备openJDK源码和环境 1.在linux和macOS上编译openJDK更加友好, ...

  8. 5阶m序列

    void echo32(int m) { printf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d\n ...

  9. Vue post提交

    vue中的axios 是不直接支持post方法的,所以我们得绕一下路,我目前在登录的时候运用到了,服务器端用php,可以收到数据 let param = new URLSearchParams(); ...

  10. delphi 中的浮点数 (转载)

    原文地址 Floating point numbers — Sand or dirt Floating point numbers are like piles of sand; every time ...