IwbZeroModule.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.Linq;
  2. using System.Reflection;
  3. using Abp;
  4. using Abp.Collections.Extensions;
  5. using Abp.Configuration.Startup;
  6. using Abp.Dependency;
  7. using Abp.Localization;
  8. using Abp.Localization.Dictionaries;
  9. using Abp.Localization.Dictionaries.Xml;
  10. using Abp.Modules;
  11. using Abp.MultiTenancy;
  12. using Abp.Reflection;
  13. using Abp.Reflection.Extensions;
  14. using IwbZero.Authorization.Base.Roles;
  15. using IwbZero.Authorization.Base.SystemInfo;
  16. using IwbZero.Authorization.Base.Users;
  17. using IwbZero.MultiTenancy;
  18. using IwbZero.Zero.Configuration;
  19. namespace IwbZero
  20. {
  21. [DependsOn(typeof(AbpKernelModule))]
  22. public class IwbZeroModule : AbpModule
  23. {
  24. public override void PreInitialize()
  25. {
  26. Configuration.BackgroundJobs.IsJobExecutionEnabled = true;
  27. IocManager.RegisterIfNot<IIwbZeroEntityTypes, IwbZeroEntityTypes>(); //Registered on services.AddAbpIdentity() for Abp.ZeroCore.
  28. IocManager.Register<IRoleManagementConfig, RoleManagementConfig>();
  29. IocManager.Register<IUserManagementConfig, UserManagementConfig>();
  30. IocManager.Register<ILanguageManagementConfig, LanguageManagementConfig>();
  31. IocManager.Register<IIwbZeroConfig, IwbZeroConfig>();
  32. Configuration.ReplaceService<ITenantStore, TenantStore>(DependencyLifeStyle.Transient);
  33. Configuration.Settings.Providers.Add<IwbZeroSettingProvider>();
  34. foreach (var languageInfo in Configuration.Localization.Languages)
  35. {
  36. Configuration.Localization.Languages.Remove(languageInfo);
  37. }
  38. Configuration.Localization.Languages.Add(new LanguageInfo("zh-Hans", "简体中文", "cn", true));
  39. Configuration.Localization.Languages.Add(new LanguageInfo("en", "English", "us"));
  40. Configuration.Localization.Sources.Add(
  41. new DictionaryBasedLocalizationSource(
  42. IwbZeroConsts.LocalizationSourceName,
  43. new XmlEmbeddedFileLocalizationDictionaryProvider(
  44. typeof(IwbZeroModule).GetAssembly(), "IwbZero.Zero.Localization.Source"
  45. )));
  46. AddIgnoredTypes();
  47. }
  48. public override void Initialize()
  49. {
  50. FillMissingEntityTypes();
  51. IocManager.RegisterAssemblyByConvention(typeof(IwbZeroModule).GetAssembly());
  52. RegisterTenantCache();
  53. }
  54. private void AddIgnoredTypes()
  55. {
  56. var ignoredTypes = new[]
  57. {
  58. typeof(SysLog)
  59. };
  60. foreach (var ignoredType in ignoredTypes)
  61. {
  62. Configuration.EntityHistory.IgnoredTypes.AddIfNotContains(ignoredType);
  63. }
  64. }
  65. private void FillMissingEntityTypes()
  66. {
  67. using (var entityTypes = IocManager.ResolveAsDisposable<IIwbZeroEntityTypes>())
  68. {
  69. if (entityTypes.Object.User != null &&
  70. entityTypes.Object.Role != null &&
  71. entityTypes.Object.Tenant != null)
  72. {
  73. return;
  74. }
  75. using (var typeFinder = IocManager.ResolveAsDisposable<ITypeFinder>())
  76. {
  77. var types = typeFinder.Object.FindAll();
  78. entityTypes.Object.Tenant = types.FirstOrDefault(t => typeof(TenantBase).IsAssignableFrom(t) && !t.GetTypeInfo().IsAbstract);
  79. entityTypes.Object.Role = types.FirstOrDefault(t => typeof(RoleBase).IsAssignableFrom(t) && !t.GetTypeInfo().IsAbstract);
  80. entityTypes.Object.User = types.FirstOrDefault(t => typeof(UserBase).IsAssignableFrom(t) && !t.GetTypeInfo().IsAbstract);
  81. entityTypes.Object.Function = types.FirstOrDefault(t => typeof(SysFunctionBase).IsAssignableFrom(t) && !t.GetTypeInfo().IsAbstract);
  82. entityTypes.Object.State = types.FirstOrDefault(t => typeof(SysStateBase).IsAssignableFrom(t) && !t.GetTypeInfo().IsAbstract);
  83. entityTypes.Object.AttachFile = types.FirstOrDefault(t => typeof(SysAttachFileBase).IsAssignableFrom(t) && !t.GetTypeInfo().IsAbstract);
  84. entityTypes.Object.Help = types.FirstOrDefault(t => typeof(SysHelpBase).IsAssignableFrom(t) && !t.GetTypeInfo().IsAbstract);
  85. }
  86. }
  87. }
  88. private void RegisterTenantCache()
  89. {
  90. if (IocManager.IsRegistered<ITenantCache>())
  91. {
  92. return;
  93. }
  94. using (var entityTypes = IocManager.ResolveAsDisposable<IIwbZeroEntityTypes>())
  95. {
  96. var implType = typeof(TenantCache<,>)
  97. .MakeGenericType(entityTypes.Object.Tenant, entityTypes.Object.User);
  98. IocManager.Register(typeof(ITenantCache), implType, DependencyLifeStyle.Transient);
  99. }
  100. }
  101. }
  102. }