Autofac的基本使用---目录

准备

使用的表是Student,创建相关的IDAL、DAL、IBLL、BLL层。

使用EF,创建一个Model层,存放edmx文件。

创建一个Infrastructure层,基础设施项目,使用泛型类型。

普通类型的使用

1.APP.Config 配置信息

此处有个需要注意的地方:configSections节点必须要放在configuration节点的最上面,否则运行会报错。这个在MSDN上有说明。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="autofac" type="Autofac.Configuration.SectionHandler,Autofac.Configuration"></section>
</configSections>
<autofac configSource="XmlConfig\autofac.config" />
<connectionStrings>
<add name="AutofacDBEntities" connectionString="metadata=res://*/AutofacDB.csdl|res://*/AutofacDB.ssdl|res://*/AutofacDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=AutofacDB;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

2.Autofac.config配置信息

<?xml version="1.0" encoding="utf-8"?>
<autofac>
<components>
<!--普通类型Student-->
<component type="Apps.DAL.StudentDAL,Apps.DAL" service="Apps.IDAL.IStudentDAL,Apps.IDAL" />
<component type="Apps.BLL.StudentBLL,Apps.BLL" service="Apps.IBLL.IStudentBLL,Apps.IBLL" />
</components>
</autofac>

3.控制台程序代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Apps.BLL;
using Apps.DAL;
using Apps.IBLL;
using Apps.IDAL;
using Apps.Infrastructure.BaseObject;
using Apps.Infrastructure.IBaseInterface;
using Apps.Model;
using Autofac;
using Autofac.Configuration; namespace Apps.Con
{
class Program
{
static void Main(string[] args)
{
#region 普通类型---Student---Config获取配置
var builder = new ContainerBuilder();
builder.RegisterModule(new ConfigurationSettingsReader("autofac")); // 编译容器完成注册且准备对象解析
var container = builder.Build(); // 现在你可以使用 Autofac 解析服务. 例如,这行将执行注册的lambda表达式对于 IConfigReader 服务.
//但是我们不推荐直接操作容器,这会导致内存泄漏。
//当我们解析出一个组件时,依赖于我们定义的lifetime scope,一个新的对象实例会被创建。
using (var scope = container.BeginLifetimeScope())
{
//从容器中解析需要使用的组件
var iStudentBLL = scope.Resolve<IStudentBLL>();
//调用解析后的组件中的方法
List<Student> list = iStudentBLL.GetList().ToList(); Console.WriteLine("List中的数据行:" + list.Count);
}
#endregion Console.ReadKey();
}
}
}

(1)使用流程

a.参见Autofac管理注册类的容器实例

  var builder = new ContainerBuilder();

b.下面就需要为这个容器注册它可以管理的类型

  builder.RegisterType<StudentDAL>().As<IStudentDAL>();

c.注册泛型,这个地方需要把泛型进行注册,否则无法正常执行

  builder.RegisterGeneric(typeof(UnitOfWork<>)).As(typeof(IUnitOfWork<>)).SingleInstance();

  或

  builder.RegisterType<UnitOfWork<AutofacDBEntities>>().As<IUnitOfWork<AutofacDBEntities>>().SingleInstance();

d.生成具体的实例

  var container = builder.Build();

e.在应用运行期间,你需要从容器生命周期域中解析出组件实例来使用它们。

  using (var scope = container.BeginLifetimeScope())  {  }

f.从容器中解析需要使用的组件

  var iStudentBLL = scope.Resolve<IStudentBLL>();

g.调用解析出来的组件的方法

  List<Student> list = iStudentBLL.GetList().ToList();

(2)中间碰到的问题。

在官方文档,http://docs.autofac.org/en/latest/configuration/xml.html#configuring-with-application-configuration-legacy-pre-4-0

其中的配置示例,component节点中。

type属性,是 类完整命名空间(包括类名),类的命名空间(不包括类名)

service属性,是 类完整命名空间(包括类名)

与示例中的区别就是,示例中的service属性值 与type保持一致,均有","逗号 后加类的命名空间。而官方文档中却没有。

当示例也这么编写时,运行就报错了。

<autofac defaultAssembly="Autofac.Example.Calculator.Api">
<components>
<component
type="Autofac.Example.Calculator.Addition.Add, Autofac.Example.Calculator.Addition"
service="Autofac.Example.Calculator.Api.IOperation" /> <component
type="Autofac.Example.Calculator.Division.Divide, Autofac.Example.Calculator.Division"
service="Autofac.Example.Calculator.Api.IOperation" >
<parameters>
<parameter name="places" value="4" />
</parameters>
</component>
</components>
</autofac>

泛型类型的使用

当在autofac.config配置文件中,如此配置。

泛型类型的配置失败,无法正常运行,随之放弃。

不知道如何进行泛型类型的配置设置。

<?xml version="1.0" encoding="utf-8"?>
<autofac>
<components>
<!--普通类型Student-->
<!--<component type="Apps.DAL.StudentDAL,Apps.DAL" service="Apps.IDAL.IStudentDAL,Apps.IDAL" />
<component type="Apps.BLL.StudentBLL,Apps.BLL" service="Apps.IBLL.IStudentBLL,Apps.IBLL" />-->
<!--泛型类型Teacher-->
<component
type="Apps.Infrastructure.BaseObject.UnitOfWork,Apps.Infrastructure.BaseObject"
service="Apps.Infrastructure.IBaseObject.IUnitOfWork,Apps.Infrastructure.IBaseObject" />
<component
type="Apps.Infrastructure.BaseObject.DatabaseFactory, Apps.Infrastructure.BaseObject"
service="Apps.Infrastructure.IBaseObject.IDatabaseFactory,Apps.Infrastructure.IBaseObject" />
<component type="Apps.DAL.TeacherDAL,Apps.DAL" service="Apps.IDAL.ITeacherDAL" />
<component type="Apps.BLL.TeacherBLL,Apps.BLL" service="Apps.IBLL.ITeacherBLL" />
</components>
</autofac>

Autofac的基本使用---4、使用Config配置的更多相关文章

  1. Web.config配置数据库连接

    web.config配置数据库连接   第一种:取连接字符串 string connString = System.Web.Configuration.WebConfigurationManager. ...

  2. Asp.net Web.Config - 配置元素 caching

    Asp.net Web.Config - 配置元素 caching 记得之前在写缓存DEMO的时候,好像配置过这个元素,好像这个元素还有点常用. 一.caching元素列表   元素 说明 cache ...

  3. asp.net中web.config配置节点大全详解

    最近网上找了一些关于Web.config配置节点的文章,发现很多都写的都比较零散,而且很少有说明各个配置节点的作用和用法.搜索了一下发现有一篇写的不错,这里引用一下 原文地址 http://www.c ...

  4. 十五天精通WCF——第二天 告别烦恼的config配置

    经常搞wcf的基友们肯定会知道,当你的应用程序有很多的“服务引用”的时候,是不是有一种疯狂的感觉...从一个环境迁移到另外一个环境,你需要改变的 endpoint会超级tmd的多,简直就是搞死了人.. ...

  5. web.config配置详细说明

    (一).Web.Config是以XML文件规范存储,配置文件分为以下格式 1.配置节处理程序声明    特点:位于配置文件的顶部,包含在<configSections>标志中. 2.特定应 ...

  6. EF连接MySQL数据Web.Config配置

    EF连接MySQL数据Web.Config配置 <?xml version="1.0" encoding="utf-8"?> <configu ...

  7. app.config 配置多项 配置集合 自定义配置(3)

    再说说利用app.config配置多个自定义的方法.先看这个例子:美国家庭Simpson的家里有父亲母亲和三个儿女,而中国的老王只有独生子女.结构如下: <?xml version=" ...

  8. springcloud之config配置中心-Finchley.SR2版

    本篇和大家分享的是springcloud-config配置中心搭建,写到这里突然想起自己曾今开源过基于Redis发布订阅编写的一个配置中心,刚看了git星数有点少哈哈,这里顺势发个连接欢迎大侠们点赞: ...

  9. iis设置默认文档,提示web.config配置xml格式不正确

    网站上传后,配置默认文档,提示web.config配置xml格式不正确,几经尝试,发现是sqlserver密码中的“&”符号惹的祸,web.config文件中不能使用该字符.分享出来,大家遇到 ...

随机推荐

  1. 简化的鸿蒙WiFi接口,仅需几行代码,简单易用!

    使用鸿蒙原始WiFI API接口进行编程,整个过程稍显繁琐,为此我们对鸿蒙原始WiFi API接口做了一层封装,形成了一套更简单易用的接口. 简化后的API接口 STA模式 // 连接WiFi热点,并 ...

  2. NOIP2015 解题报告

    Day1 T3 运输计划 二分之后做一遍树上差分,找出被所有时间超限的运输计划覆盖的花费时间最长的航道,将其改造成虫洞. LCA 用倍增求可能会被卡常,建议用 Tarjan 求.

  3. MySQL的两种日志类型,redo log,binlog

    文章内容学习:极客时间-林晓彬老师-MySQL实战45讲 整理而得 我们知道MySQL数据库在发生意外宕机的情况下,可以将数据恢复到历史的某个时间点,能实现这个功能依靠的是日志,MySQL提供两种类型 ...

  4. equals的推荐写法

    我们在重写equals的时候必须满足几个原则,否则,类在容器和其他场景下会出现奇怪的行为: 1.A.equals(A)=true, 2.对称性.即A.equals(B)=true;则B.equals( ...

  5. BloomFilte布隆过滤器简介

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...

  6. CobaltStrike 生成office宏病毒进行钓鱼攻击

    关于WORD宏: 在百度百科上有: 宏是一个批量处理程序命令,正确地运用它可以提高工作效率.微软的office软件允许用户自己编写,叫VBA的脚本来增加其灵活性,进一步扩充它的能力.如完打开word文 ...

  7. JS "&&"操作符妙用

    首先来了解一下 "&&"操作符的工作原理: "&&"连接两个表达式,当两侧表达式都为真时,返回TRUE.有一个为假则返回FALS ...

  8. 【题解】「CF1182B」Plus from Picture

    这是一道超级水的模拟 + 简单搜索. 说说思路: 先找到中心点,就是自己和上下左右都为 * 的. 上下左右上的所有 * 都删掉,然后再看看有没有多余的 * 如果有输出 NO 否则输出 YES. 比如说 ...

  9. BJOI2015 隐身术

    落谷. Description 给你两个串 \(A.B\).询问 \(B\) 中有多少个非空子串和 \(A\) 的编辑距离不超过 \(K\). Solution 发现 \(K \le 5\),考虑可以 ...

  10. 落谷 P1412 经营与开发

    题目链接 Solution 用传统的思想考虑正推,发现后面的答案依赖于当前的 \(p\),你不但要记录前 \(i\) 个还要记录 \(p\),显然空间爆炸. 类似 AcWing 300. 任务安排1, ...