RegisterModule.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Autofac;
  2. using Autofac.Extras.DynamicProxy;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using Module = Autofac.Module;
  8. using Microsoft.Extensions.DependencyModel;
  9. using ZhonTai.Admin.Core.Configs;
  10. using ZhonTai.Admin.Core.Db.Transaction;
  11. using ZhonTai.Admin.Core.Attributes;
  12. using ZhonTai.Admin.Core.Repositories;
  13. using Microsoft.AspNetCore.Identity;
  14. namespace ZhonTai.Admin.Core.RegisterModules;
  15. public class RegisterModule : Module
  16. {
  17. private readonly AppConfig _appConfig;
  18. /// <summary>
  19. /// 模块注入
  20. /// </summary>
  21. /// <param name="appConfig">AppConfig</param>
  22. public RegisterModule(AppConfig appConfig)
  23. {
  24. _appConfig = appConfig;
  25. }
  26. protected override void Load(ContainerBuilder builder)
  27. {
  28. //事务拦截
  29. var interceptorServiceTypes = new List<Type>();
  30. if (_appConfig.Aop.Transaction)
  31. {
  32. builder.RegisterType<TransactionInterceptor>();
  33. builder.RegisterType<TransactionAsyncInterceptor>();
  34. interceptorServiceTypes.Add(typeof(TransactionInterceptor));
  35. }
  36. if(_appConfig.AssemblyNames?.Length > 0)
  37. {
  38. //程序集
  39. Assembly[] assemblies = DependencyContext.Default.RuntimeLibraries
  40. .Where(a => _appConfig.AssemblyNames.Contains(a.Name))
  41. .Select(o => Assembly.Load(new AssemblyName(o.Name))).ToArray();
  42. var nonRegisterIOCAttribute = typeof(NonRegisterIOCAttribute);
  43. var iRegisterIOCType = typeof(IRegisterIOC);
  44. bool Predicate(Type a) => !a.IsDefined(nonRegisterIOCAttribute, true)
  45. && (a.Name.EndsWith("Service") || a.Name.EndsWith("Repository") || iRegisterIOCType.IsAssignableFrom(a))
  46. && !a.IsAbstract && !a.IsInterface && a.IsPublic;
  47. //有接口实例
  48. builder.RegisterAssemblyTypes(assemblies)
  49. .Where(Predicate)
  50. .AsImplementedInterfaces()
  51. .InstancePerLifetimeScope()
  52. .PropertiesAutowired()// 属性注入
  53. .InterceptedBy(interceptorServiceTypes.ToArray())
  54. .EnableInterfaceInterceptors();
  55. //无接口实例
  56. builder.RegisterAssemblyTypes(assemblies)
  57. .Where(Predicate)
  58. .InstancePerLifetimeScope()
  59. .PropertiesAutowired()// 属性注入
  60. .InterceptedBy(interceptorServiceTypes.ToArray())
  61. .EnableClassInterceptors();
  62. if(_appConfig.PasswordHasher)
  63. builder.RegisterGeneric(typeof(PasswordHasher<>)).As(typeof(IPasswordHasher<>)).SingleInstance().PropertiesAutowired();
  64. //仓储泛型注入
  65. builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IRepositoryBase<>)).InstancePerLifetimeScope().PropertiesAutowired();
  66. builder.RegisterGeneric(typeof(RepositoryBase<,>)).As(typeof(IRepositoryBase<,>)).InstancePerLifetimeScope().PropertiesAutowired();
  67. }
  68. }
  69. }