[Abp 源码分析]五、系统设置 (2)

其实我们转到 ISmtpEmailSenderConfiguration 的实现 SmtpEmailSenderConfiguration 就清楚了。

public class SmtpEmailSenderConfiguration : EmailSenderConfiguration, ISmtpEmailSenderConfiguration, ITransientDependency { /// <summary> /// SMTP Host name/IP. /// </summary> public virtual string Host { get { return GetNotEmptySettingValue(EmailSettingNames.Smtp.Host); } } /// <summary> /// SMTP Port. /// </summary> public virtual int Port { get { return SettingManager.GetSettingValue<int>(EmailSettingNames.Smtp.Port); } } /// <summary> /// User name to login to SMTP server. /// </summary> public virtual string UserName { get { return GetNotEmptySettingValue(EmailSettingNames.Smtp.UserName); } } /// <summary> /// Password to login to SMTP server. /// </summary> public virtual string Password { get { return GetNotEmptySettingValue(EmailSettingNames.Smtp.Password); } } /// <summary> /// Domain name to login to SMTP server. /// </summary> public virtual string Domain { get { return SettingManager.GetSettingValue(EmailSettingNames.Smtp.Domain); } } /// <summary> /// Is SSL enabled? /// </summary> public virtual bool EnableSsl { get { return SettingManager.GetSettingValue<bool>(EmailSettingNames.Smtp.EnableSsl); } } /// <summary> /// Use default credentials? /// </summary> public virtual bool UseDefaultCredentials { get { return SettingManager.GetSettingValue<bool>(EmailSettingNames.Smtp.UseDefaultCredentials); } } /// <summary> /// Creates a new <see cref="SmtpEmailSenderConfiguration"/>. /// </summary> /// <param>Setting manager</param> public SmtpEmailSenderConfiguration(ISettingManager settingManager) : base(settingManager) { } }

在这里我们可以看到这些配置项其实是通过一个名字叫做 GetNotEmptySettingValue() 的方法来得到的,该方法定义在 SmtpEmailSenderConfiguration 的基类 EmailSenderConfiguration 当中。

public abstract class EmailSenderConfiguration : IEmailSenderConfiguration { // 其他代码,已经省略 /// <summary> /// Creates a new <see cref="EmailSenderConfiguration"/>. /// </summary> protected EmailSenderConfiguration(ISettingManager settingManager) { SettingManager = settingManager; } /// <summary> /// Gets a setting value by checking. Throws <see cref="AbpException"/> if it's null or empty. /// </summary> /// <param>Name of the setting</param> /// <returns>Value of the setting</returns> protected string GetNotEmptySettingValue(string name) { var value = SettingManager.GetSettingValue(name); if (value.IsNullOrEmpty()) { throw new AbpException($"Setting value for '{name}' is null or empty!"); } return value; } }

总而言之,如果你想要获取已经添加好的设置项,直接注入 ISettingManager 通过其 GetSettingValue() 就可以拿到这些设置项。

1.2 代码分析

Abp 系统设置相关的最核心的部分就是 ISettingManager、ISettingDefinitionManager、ISettingStore,SettingProvider、SettingDefinition 下面就这几个类进行一些细致的解析。

1.2.1 SettingDefinition

在 Abp 当中,一个设置项就是一个 SettingDefinition,每个 SettingDefinition 的 Name 与 Value 是必填的,其中 Scopes 字段对应一个 SettingScopes 枚举,该属性用于确定这个设置项的使用应用范围。

public class SettingDefinition { /// <summary> /// Unique name of the setting. /// </summary> public string Name { get; private set; } /// <summary> /// Display name of the setting. /// This can be used to show setting to the user. /// </summary> public ILocalizableString DisplayName { get; set; } /// <summary> /// A brief description for this setting. /// </summary> public ILocalizableString Description { get; set; } /// <summary> /// Scopes of this setting. /// Default value: <see cref="SettingScopes.Application"/>. /// </summary> public SettingScopes Scopes { get; set; } /// <summary> /// Is this setting inherited from parent scopes. /// Default: True. /// </summary> public bool IsInherited { get; set; } /// <summary> /// Gets/sets group for this setting. /// </summary> public SettingDefinitionGroup Group { get; set; } /// <summary> /// Default value of the setting. /// </summary> public string DefaultValue { get; set; } /// <summary> /// Can clients see this setting and it's value. /// It maybe dangerous for some settings to be visible to clients (such as email server password). /// Default: false. /// </summary> [Obsolete("Use ClientVisibilityProvider instead.")] public bool IsVisibleToClients { get; set; } /// <summary> /// Client visibility definition for the setting. /// </summary> public ISettingClientVisibilityProvider ClientVisibilityProvider { get; set; } /// <summary> /// Can be used to store a custom object related to this setting. /// </summary> public object CustomData { get; set; } public SettingDefinition( string name, string defaultValue, ILocalizableString displayName = null, SettingDefinitionGroup group = null, ILocalizableString description = null, SettingScopes scopes = SettingScopes.Application, bool isVisibleToClients = false, bool isInherited = true, object customData = null, ISettingClientVisibilityProvider clientVisibilityProvider = null) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } Name = name; DefaultValue = defaultValue; DisplayName = displayName; Group = @group; Description = description; Scopes = scopes; IsVisibleToClients = isVisibleToClients; IsInherited = isInherited; CustomData = customData; ClientVisibilityProvider = new HiddenSettingClientVisibilityProvider(); if (isVisibleToClients) { ClientVisibilityProvider = new VisibleSettingClientVisibilityProvider(); } else if (clientVisibilityProvider != null) { ClientVisibilityProvider = clientVisibilityProvider; } } } 1.2.2 ISettingManager

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssfxp.html