DALModule.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Reflection;
  6. using System.Xml.Linq;
  7. using Autofac;
  8. using EMIS.DataLogic;
  9. using EMIS.Utility;
  10. using Bowin.Common.XML;
  11. namespace EMIS.CommonLogic
  12. {
  13. public class DALModule : Autofac.Module
  14. {
  15. private Dictionary<string, string> iocConfigList;
  16. private Dictionary<string, string> IocConfigList
  17. {
  18. get
  19. {
  20. if (iocConfigList == null)
  21. {
  22. string configPath = HttpContext.Current.Server.MapPath("~/Config/IOCConfig.xml");
  23. XElement config = XElement.Load(new System.IO.FileStream(configPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read), LoadOptions.None);
  24. if (config.Elements().Count() == 0)
  25. {
  26. iocConfigList = new Dictionary<string, string>();
  27. return iocConfigList;
  28. }
  29. iocConfigList = (from x in config.Elements("component")
  30. select new
  31. {
  32. FullName = x.Attribute("name").GetStringValue(),
  33. Implement = x.Attribute("implement").GetStringValue()
  34. }).ToDictionary(x => x.FullName, x => x.Implement);
  35. }
  36. return iocConfigList;
  37. }
  38. }
  39. protected override void Load(ContainerBuilder builder)
  40. {
  41. builder.RegisterType<UnitOfWork>().InstancePerLifetimeScope();
  42. builder.RegisterType<HRUnitOfWork>().InstancePerLifetimeScope();
  43. builder.RegisterType<LogUnitOfWork>().InstancePerLifetimeScope();
  44. var blAssembly = typeof(EMIS.DataLogic.UnitOfWork).Assembly;
  45. builder.RegisterAssemblyTypes(blAssembly)
  46. .Where(x => x.Name.EndsWith("Repository") && x.BaseType.GetGenericArguments()[0].FullName.StartsWith("EMIS.Entities"))
  47. .AsSelf()
  48. .WithParameter((pi, c) => pi.ParameterType == typeof(UnitOfWork),
  49. (pi, c) => c.Resolve<UnitOfWork>())
  50. .WithParameter((pi, c) => pi.ParameterType == typeof(HRUnitOfWork),
  51. (pi, c) => c.Resolve<HRUnitOfWork>())
  52. .WithParameter((pi, c) => pi.ParameterType == typeof(LogUnitOfWork),
  53. (pi, c) => c.Resolve<LogUnitOfWork>())
  54. .PropertiesAutowired()
  55. .InstancePerLifetimeScope();
  56. //情况1:注册没有被配置过的组件
  57. builder.RegisterAssemblyTypes(blAssembly)
  58. .Where(x => x.FullName.StartsWith("EMIS.DataLogic") && x.Name.EndsWith("DAL") && !IocConfigList.ContainsKey(x.Namespace + "." + x.Name))
  59. .AsSelf()
  60. .PropertiesAutowired()
  61. .InstancePerLifetimeScope();
  62. //情况2:注册配置的组件;
  63. //如果后续有空,可以研究一下Autofac.Configuration.dll里面的读取配置的方式,如果成功可以简化掉这堆代码,调用的方式其实不难,
  64. //主要担心就是Autofac读取配置进行注册是会覆盖掉原有的设置,还是多注册一个映射类,我们需要的是覆盖掉的效果。
  65. var configuratedComponentEnum = (from ic in IocConfigList
  66. select new { ic.Key, Value = ic.Value.Split(',') } into fic
  67. select new { SourceClass = fic.Key, Assembly = fic.Value[1].Trim(), Class = fic.Value[0].Trim() } into aic
  68. group aic by aic.Assembly into g
  69. select new { Assembly = g.Key, Classes = g.Select(x => new { x.Class, SourceClass = x.SourceClass }).ToList() });
  70. var configuratedComponentList = configuratedComponentEnum.ToList();
  71. foreach (var configuratedComponent in configuratedComponentList)
  72. {
  73. var assembly = Assembly.Load(configuratedComponent.Assembly);
  74. if (assembly == null)
  75. {
  76. continue;
  77. }
  78. foreach (var myClass in configuratedComponent.Classes)
  79. {
  80. var sourceType = blAssembly.GetType(myClass.SourceClass);
  81. var classType = assembly.GetType(myClass.Class);
  82. if (classType == null)
  83. {
  84. builder.RegisterAssemblyTypes(blAssembly)
  85. .Where(x => (x.Namespace + "." + x.Name) == myClass.SourceClass)
  86. .AsSelf()
  87. .PropertiesAutowired()
  88. .InstancePerLifetimeScope();
  89. continue;
  90. }
  91. if (sourceType != null)
  92. {
  93. builder.RegisterType(classType).As(sourceType)
  94. .PropertiesAutowired()
  95. .InstancePerLifetimeScope();
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }