AbpModule.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Abp.Collections.Extensions;
  6. using Abp.Configuration.Startup;
  7. using Abp.Dependency;
  8. using Castle.Core.Logging;
  9. namespace Abp.Modules
  10. {
  11. /// <summary>
  12. /// This class must be implemented by all module definition classes.
  13. /// </summary>
  14. /// <remarks>
  15. /// A module definition class is generally located in its own assembly
  16. /// and implements some action in module events on application startup and shutdown.
  17. /// It also defines depended modules.
  18. /// </remarks>
  19. public abstract class AbpModule
  20. {
  21. /// <summary>
  22. /// Gets a reference to the IOC manager.
  23. /// </summary>
  24. protected internal IIocManager IocManager { get; internal set; }
  25. /// <summary>
  26. /// Gets a reference to the ABP configuration.
  27. /// </summary>
  28. protected internal IAbpStartupConfiguration Configuration { get; internal set; }
  29. /// <summary>
  30. /// Gets or sets the logger.
  31. /// </summary>
  32. public ILogger Logger { get; set; }
  33. protected AbpModule()
  34. {
  35. Logger = NullLogger.Instance;
  36. }
  37. /// <summary>
  38. /// This is the first event called on application startup.
  39. /// Codes can be placed here to run before dependency injection registrations.
  40. /// </summary>
  41. public virtual void PreInitialize()
  42. {
  43. }
  44. /// <summary>
  45. /// This method is used to register dependencies for this module.
  46. /// </summary>
  47. public virtual void Initialize()
  48. {
  49. }
  50. /// <summary>
  51. /// This method is called lastly on application startup.
  52. /// </summary>
  53. public virtual void PostInitialize()
  54. {
  55. }
  56. /// <summary>
  57. /// This method is called when the application is being shutdown.
  58. /// </summary>
  59. public virtual void Shutdown()
  60. {
  61. }
  62. public virtual Assembly[] GetAdditionalAssemblies()
  63. {
  64. return new Assembly[0];
  65. }
  66. /// <summary>
  67. /// Checks if given type is an Abp module class.
  68. /// </summary>
  69. /// <param name="type">Type to check</param>
  70. public static bool IsAbpModule(Type type)
  71. {
  72. var typeInfo = type.GetTypeInfo();
  73. return
  74. typeInfo.IsClass &&
  75. !typeInfo.IsAbstract &&
  76. !typeInfo.IsGenericType &&
  77. typeof(AbpModule).IsAssignableFrom(type);
  78. }
  79. /// <summary>
  80. /// Finds direct depended modules of a module (excluding given module).
  81. /// </summary>
  82. public static List<Type> FindDependedModuleTypes(Type moduleType)
  83. {
  84. if (!IsAbpModule(moduleType))
  85. {
  86. throw new AbpInitializationException("This type is not an ABP module: " + moduleType.AssemblyQualifiedName);
  87. }
  88. var list = new List<Type>();
  89. if (moduleType.GetTypeInfo().IsDefined(typeof(DependsOnAttribute), true))
  90. {
  91. var dependsOnAttributes = moduleType.GetTypeInfo().GetCustomAttributes(typeof(DependsOnAttribute), true).Cast<DependsOnAttribute>();
  92. foreach (var dependsOnAttribute in dependsOnAttributes)
  93. {
  94. foreach (var dependedModuleType in dependsOnAttribute.DependedModuleTypes)
  95. {
  96. list.Add(dependedModuleType);
  97. }
  98. }
  99. }
  100. return list;
  101. }
  102. public static List<Type> FindDependedModuleTypesRecursivelyIncludingGivenModule(Type moduleType)
  103. {
  104. var list = new List<Type>();
  105. AddModuleAndDependenciesRecursively(list, moduleType);
  106. list.AddIfNotContains(typeof(AbpKernelModule));
  107. return list;
  108. }
  109. private static void AddModuleAndDependenciesRecursively(List<Type> modules, Type module)
  110. {
  111. if (!IsAbpModule(module))
  112. {
  113. throw new AbpInitializationException("This type is not an ABP module: " + module.AssemblyQualifiedName);
  114. }
  115. if (modules.Contains(module))
  116. {
  117. return;
  118. }
  119. modules.Add(module);
  120. var dependedModules = FindDependedModuleTypes(module);
  121. foreach (var dependedModule in dependedModules)
  122. {
  123. AddModuleAndDependenciesRecursively(modules, dependedModule);
  124. }
  125. }
  126. }
  127. }