123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 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<string, string> iocConfigList;
- private Dictionary<string, string> 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<string, string>();
- 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<UnitOfWork>().InstancePerLifetimeScope();
- builder.RegisterType<HRUnitOfWork>().InstancePerLifetimeScope();
- builder.RegisterType<LogUnitOfWork>().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<UnitOfWork>())
- .WithParameter((pi, c) => pi.ParameterType == typeof(HRUnitOfWork),
- (pi, c) => c.Resolve<HRUnitOfWork>())
- .WithParameter((pi, c) => pi.ParameterType == typeof(LogUnitOfWork),
- (pi, c) => c.Resolve<LogUnitOfWork>())
- .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();
- }
- }
- }
- }
- }
- }
|