123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using Autofac;
- using System.Reflection;
- using Autofac.Core;
- using EMIS.DataLogic;
- using EMIS.Utility;
- using Bowin.Common.XML;
- using System.Xml.Linq;
- using EMIS.CommonLogic.SystemServices;
- using System.Configuration;
- using EMIS.ViewModel;
- namespace EMIS.CommonLogic
- {
- public class ServiceModule : 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;
- }
- }
- private Dictionary<string, string> fileUploadConfigList;
- private Dictionary<string, string> FileUploadConfigList
- {
- get
- {
- if (fileUploadConfigList == null)
- {
- var config = (FileUploadConfig)ConfigurationManager.GetSection("fileUploadConfig");
- fileUploadConfigList = config.ServiceTypes;
- }
- return fileUploadConfigList;
- }
- }
- private string GetFileUploadServicesType(Type type)
- {
- if (FileUploadConfigList.ContainsKey(type.FullName))
- {
- return FileUploadConfigList[type.FullName];
- }
- return null;
- }
- protected override void Load(ContainerBuilder builder)
- {
- //通用类型注册
- //情况1:注册没有被配置过的组件
- var blAssembly = typeof(BaseServices).Assembly;
- builder.RegisterAssemblyTypes(blAssembly)
- .Where(x => x.FullName.StartsWith("EMIS.CommonLogic") &&
- x.Name != "BaseServices" &&
- x.Name.EndsWith("Services") && !x.GetInterfaces().Any(w => IocConfigList.ContainsKey(w.Namespace + "." + w.Name)))
- .AsImplementedInterfaces()
- .PropertiesAutowired()
- .InstancePerLifetimeScope();
- var uploadFileServicesList = builder.RegisterTypes(blAssembly.GetTypes().Where(x => x.FullName.StartsWith("EMIS.CommonLogic") &&
- x.Name != "BaseServices" && x.GetInterfaces().Any(w => w.Name == "IFileUploadServices" && w.Namespace == "EMIS.CommonLogic.SystemServices")
- && FileUploadConfigList.ContainsKey(x.FullName)).ToArray());
- uploadFileServicesList.Named<IFileUploadServices>(GetFileUploadServicesType)
- .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 { Interface = 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, x.Interface }).ToList() });
- var configuratedComponentList = configuratedComponentEnum.ToList();
- var iblAssembly = typeof(IBaseServices).Assembly;
- foreach (var configuratedComponent in configuratedComponentList)
- {
- var assembly = Assembly.Load(configuratedComponent.Assembly);
- if (assembly == null)
- {
- continue;
- }
- foreach (var myClass in configuratedComponent.Classes)
- {
- var interfaceType = iblAssembly.GetType(myClass.Interface);
- var classType = assembly.GetType(myClass.Class);
- if (classType == null)
- {
- if (interfaceType != null)
- {
- builder.RegisterAssemblyTypes(blAssembly)
- .Where(x => x.GetInterfaces().Any(w => (w.Namespace + "." + w.Name) == myClass.Interface))
- .As(interfaceType)
- .PropertiesAutowired()
- .InstancePerLifetimeScope();
- }
- continue;
- }
- if (interfaceType != null)
- {
- builder.RegisterType(classType).As(interfaceType)
- .PropertiesAutowired()
- .InstancePerLifetimeScope();
- }
- }
- }
- }
- }
- }
|