准備
使用的表是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>