Autofac的基本使用---4、使用Config配置
准备
使用的表是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="data source=.;initial catalog=AutofacDB;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework"" 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>

浙公网安备 33010602011771号