ABP模塊配置
介紹
ABP中一些配置都是通過模塊的Configuration屬性來配置的。例如在模塊的生命周期方法中可以進行一系列的配置 審計 MQ Redis....也可以替換一些ABP默認配置
通常我們的用戶模塊(自定義模塊)都會繼承自AbpModule,它是ABP所有模塊的基類.也是個抽象類.
public abstract class AbpModule { protected internal IIocManager IocManager { get; internal set; } protected internal IAbpStartupConfiguration Configuration { get; internal set; } // 其他代碼 }
這裏的兩個屬性分別是IIocManager和IAbpStartupConfiguration,所以我們的用戶模塊可以使用.
IocManager已經研究過了,現在來看看IAbpStartupConfiguration,先看看類關系圖
可以看到ABP一系列的基礎設施都在裏面,授權 事件總線 等等.
啟動流程
IAbpStartupConfiguration註冊,初始化其實都是在ABPBootStrap的初始化方法中執行的.
public virtual void Initialize() { // 其他代碼 try { // 註冊了相關基礎設施的配置 IocManager.IocContainer.Install(new AbpCoreInstaller()); IocManager.Resolve<AbpStartupConfiguration>().Initialize(); // 相關模塊方法 } }
可以看到首先已單例的形式註冊了一些基礎設施.然後從容器中取出AbpStartupConfiguration
執行Initialize
方法
public void Initialize() { Localization = IocManager.Resolve<ILocalizationConfiguration>(); Modules = IocManager.Resolve<IModuleConfigurations>(); Features = IocManager.Resolve<IFeatureConfiguration>(); Navigation = IocManager.Resolve<INavigationConfiguration>(); Authorization = IocManager.Resolve<IAuthorizationConfiguration>(); Validation = IocManager.Resolve<IValidationConfiguration>(); Settings = IocManager.Resolve<ISettingsConfiguration>(); UnitOfWork = IocManager.Resolve<IUnitOfWorkDefaultOptions>(); EventBus = IocManager.Resolve<IEventBusConfiguration>(); MultiTenancy = IocManager.Resolve<IMultiTenancyConfig>(); Auditing = IocManager.Resolve<IAuditingConfiguration>(); Caching = IocManager.Resolve<ICachingConfiguration>(); BackgroundJobs = IocManager.Resolve<IBackgroundJobConfiguration>(); Notifications = IocManager.Resolve<INotificationConfiguration>(); EmbeddedResources = IocManager.Resolve<IEmbeddedResourcesConfiguration>(); EntityHistory = IocManager.Resolve<IEntityHistoryConfiguration>(); CustomConfigProviders = new List<ICustomConfigProvider>(); ServiceReplaceActions = new Dictionary<Type, Action>(); }
主要是從容器中取出Configuration,為AbpStartupConfiguration對應的屬性賦值.以及初始化工作.
ServiceReplaceActions
是一個鍵值對的集合,這是我們以後替換默認基礎配置需要用到的.
Ps:IAbpStartupConfiguration是ABPModule的一個屬性,已依賴註入的方式早已經註入進容器,所以我們的自定義模塊可以很方便的對一些基礎設施做出配置.
例如Configuration.Caching.....
| Configuration.Auditing.....
等等
自定義模塊設置
internal class AbpStartupConfiguration : DictionaryBasedConfig, IAbpStartupConfiguration
可以看到我們的AbpStartupConfiguration還繼承自DictionaryBasedConfig,這個類定義如下:
namespace Abp.Configuration
{
/// <summary>
/// Used to set/get custom configuration.
/// </summary>
public class DictionaryBasedConfig : IDictionaryBasedConfig
{
/// <summary>
/// Dictionary of custom configuration.
/// </summary>
protected Dictionary<string, object> CustomSettings { get; private set; }
/// <summary>
/// Gets/sets a config value.
/// Returns null if no config with given name.
/// </summary>
/// <param name="name">Name of the config</param>
/// <returns>Value of the config</returns>
public object this[string name]
{
get { return CustomSettings.GetOrDefault(name); }
set { CustomSettings[name] = value; }
}
/// <summary>
/// Constructor.
/// </summary>
protected DictionaryBasedConfig()
{
CustomSettings = new Dictionary<string, object>();
}
/// <summary>
/// Gets a configuration value as a specific type.
/// </summary>
/// <param name="name">Name of the config</param>
/// <typeparam name="T">Type of the config</typeparam>
/// <returns>Value of the configuration or null if not found</returns>
public T Get<T>(string name)
{
var value = this[name];
return value == null
? default(T)
: (T) Convert.ChangeType(value, typeof (T));
}
/// <summary>
/// Used to set a string named configuration.
/// If there is already a configuration with same <paramref name="name"/>, it's overwritten.
/// </summary>
/// <param name="name">Unique name of the configuration</param>
/// <param name="value">Value of the configuration</param>
public void Set<T>(string name, T value)
{
this[name] = value;
}
/// <summary>
/// Gets a configuration object with given name.
/// </summary>
/// <param name="name">Unique name of the configuration</param>
/// <returns>Value of the configuration or null if not found</returns>
public object Get(string name)
{
return Get(name, null);
}
/// <summary>
/// Gets a configuration object with given name.
/// </summary>
/// <param name="name">Unique name of the configuration</param>
/// <param name="defaultValue">Default value of the object if can not found given configuration</param>
/// <returns>Value of the configuration or null if not found</returns>
public object Get(string name, object defaultValue)
{
var value = this[name];
if (value == null)
{
return defaultValue;
}
return this[name];
}
/// <summary>
/// Gets a configuration object with given name.
/// </summary>
/// <typeparam name="T">Type of the object</typeparam>
/// <param name="name">Unique name of the configuration</param>
/// <param name="defaultValue">Default value of the object if can not found given configuration</param>
/// <returns>Value of the configuration or null if not found</returns>
public T Get<T>(string name, T defaultValue)
{
return (T)Get(name, (object)defaultValue);
}
/// <summary>
/// Gets a configuration object with given name.
/// </summary>
/// <typeparam name="T">Type of the object</typeparam>
/// <param name="name">Unique name of the configuration</param>
/// <param name="creator">The function that will be called to create if given configuration is not found</param>
/// <returns>Value of the configuration or null if not found</returns>
public T GetOrCreate<T>(string name, Func<T> creator)
{
var value = Get(name);
if (value == null)
{
value = creator();
Set(name, value);
}
return (T) value;
}
}
}
ABP模塊配置