介绍
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;
}
}
}
原文地址:https://www.cnblogs.com/zzqvq/p/10283694.html