using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Reflection; using System.Xml.Linq; using Autofac; using EMIS.DataLogic; using EMIS.Utility; using Bowin.Common.XML; namespace EMIS.CommonLogic { public class DALModule : Autofac.Module { private Dictionary iocConfigList; private Dictionary IocConfigList { get { if (iocConfigList == null) { string configPath = HttpContext.Current.Server.MapPath("~/Config/IOCConfig.xml"); XElement config = XElement.Load(new System.IO.FileStream(configPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read), LoadOptions.None); if (config.Elements().Count() == 0) { iocConfigList = new Dictionary(); return iocConfigList; } iocConfigList = (from x in config.Elements("component") select new { FullName = x.Attribute("name").GetStringValue(), Implement = x.Attribute("implement").GetStringValue() }).ToDictionary(x => x.FullName, x => x.Implement); } return iocConfigList; } } protected override void Load(ContainerBuilder builder) { builder.RegisterType().InstancePerLifetimeScope(); builder.RegisterType().InstancePerLifetimeScope(); builder.RegisterType().InstancePerLifetimeScope(); var blAssembly = typeof(EMIS.DataLogic.UnitOfWork).Assembly; builder.RegisterAssemblyTypes(blAssembly) .Where(x => x.Name.EndsWith("Repository") && x.BaseType.GetGenericArguments()[0].FullName.StartsWith("EMIS.Entities")) .AsSelf() .WithParameter((pi, c) => pi.ParameterType == typeof(UnitOfWork), (pi, c) => c.Resolve()) .WithParameter((pi, c) => pi.ParameterType == typeof(HRUnitOfWork), (pi, c) => c.Resolve()) .WithParameter((pi, c) => pi.ParameterType == typeof(LogUnitOfWork), (pi, c) => c.Resolve()) .PropertiesAutowired() .InstancePerLifetimeScope(); //情况1:注册没有被配置过的组件 builder.RegisterAssemblyTypes(blAssembly) .Where(x => x.FullName.StartsWith("EMIS.DataLogic") && x.Name.EndsWith("DAL") && !IocConfigList.ContainsKey(x.Namespace + "." + x.Name)) .AsSelf() .PropertiesAutowired() .InstancePerLifetimeScope(); //情况2:注册配置的组件; //如果后续有空,可以研究一下Autofac.Configuration.dll里面的读取配置的方式,如果成功可以简化掉这堆代码,调用的方式其实不难, //主要担心就是Autofac读取配置进行注册是会覆盖掉原有的设置,还是多注册一个映射类,我们需要的是覆盖掉的效果。 var configuratedComponentEnum = (from ic in IocConfigList select new { ic.Key, Value = ic.Value.Split(',') } into fic select new { SourceClass = fic.Key, Assembly = fic.Value[1].Trim(), Class = fic.Value[0].Trim() } into aic group aic by aic.Assembly into g select new { Assembly = g.Key, Classes = g.Select(x => new { x.Class, SourceClass = x.SourceClass }).ToList() }); var configuratedComponentList = configuratedComponentEnum.ToList(); foreach (var configuratedComponent in configuratedComponentList) { var assembly = Assembly.Load(configuratedComponent.Assembly); if (assembly == null) { continue; } foreach (var myClass in configuratedComponent.Classes) { var sourceType = blAssembly.GetType(myClass.SourceClass); var classType = assembly.GetType(myClass.Class); if (classType == null) { builder.RegisterAssemblyTypes(blAssembly) .Where(x => (x.Namespace + "." + x.Name) == myClass.SourceClass) .AsSelf() .PropertiesAutowired() .InstancePerLifetimeScope(); continue; } if (sourceType != null) { builder.RegisterType(classType).As(sourceType) .PropertiesAutowired() .InstancePerLifetimeScope(); } } } } } }