我正在开发一个需要将 ConfigurationPath 和属性值保存到数据库中的应用程序。目前,如果一个类中有 3 个属性,我需要分别保存它们,我必须调用数据库来保存每个属性的更改。我不想这样保存它。这是我现有的代码。
我想使用 OptionPatterns 一次性保存或更新整个 KroneOptionsBase。
public class KroneOptionsBase
{
/// <summary>
/// The configuration key used when binding to an IConfiguration instance.
/// </summary>
public static string RootConfigurationKey => ConfigurationPath.Combine("Partners", "Krone");
}
public class PartLocatorOptions : KroneOptionsBase
{
/// <summary>
/// The configuration key used when binding to an IConfiguration instance.
/// </summary>
public static string ConfigurationKey => ConfigurationPath.Combine(RootConfigurationKey, "PartLocator");
/// <summary>
/// If the part locator is enabled or not
/// </summary>
public bool? Enabled { get; set; }
/// <summary>
/// The list of manufacturer settings
/// </summary>
public IEnumerable<ManufacturerOptions>? Manufacturers { get; set; }
}
public class ManufacturerOptions
{
/// <summary>
/// The ASPEN-specific identity for
/// the manufacturer.
/// </summary>
public int? Identity { get; set; }
/// <summary>
/// The Krone-specific brand code that
/// corresponds to the manufacturer.
/// </summary>
public string? BrandCode { get; set; }
/// <summary>
/// The identity for the associated location.
/// </summary>
public int LocationIdentity { get; set; }
}
private async Task updateMainConfigurationAsync(PartLocatorOptions configuration,
CancellationToken cancellationToken)
{
try
{
var rootKey = KroneOptionsBase.RootConfigurationKey;
var partLocatorRootKey = ConfigurationPath.Combine(rootKey, $"PartLocator");
if (_options.NotifyOnSuccess != configuration.NotifyOnSuccess)
{
await updateDatabaseLevelConfigurationAsync(partLocatorRootKey,
nameof(configuration.NotifyOnSuccess),
configuration.NotifyOnSuccess ?? false,
cancellationToken);
}
}
finally
{
_logger.LogMethodEnd();
}
}
private async Task updateDatabaseLevelConfigurationAsync(string configurationPath,
string key,
object configurationLine,
CancellationToken cancellationToken)
{
try
{
if (configurationLine != null)
{
var configurationResult = await _configurationRepository
.GetAspenDatabaseItemAsync(_databaseIdentity,
ConfigurationPath.Combine(configurationPath, key),
cancellationToken);
if (configurationLine != null && configurationLine.ToString() != configurationResult?.Value)
{
if (configurationResult == null)
{
configurationResult = new AspenDatabaseConfigurationItem
{
Key = ConfigurationPath.Combine(configurationPath, key),
DatabaseIdentity = _databaseIdentity,
Value = configurationLine.ToString()
};
_configurationRepository.AddConfigurationItem(_databaseIdentity, configurationResult);
}
else
{
configurationResult.Value = configurationLine.ToString();
await _configurationRepository.UpdateAspenDatabaseItemAsync(configurationResult, cancellationToken);
}
}
}
}
finally
{
_logger.LogMethodEnd();
}
}
例如在PartLocatorOptions我需要保存Enabled的值我必须得到像Partners:Krone:PartLocator:Enabled这样的配置路径并将其添加为KeyName并获取Enabled的值并添加它作为 ValueData.
我还面临另一个问题,例如,它包含制造商集合,那么这部分的解决方案是什么,正如您所看到的,我已经编写了一个单独的方法来将制造商列表添加到数据库中。数据库条目如下所示。见下图
据我所知,我认为保存制造商选项列表的最佳方法是将
ManufacturerOptions
分为以下列的新表:Identity、BrandCode、LocationIdentity +选项启用。
其他选项也一样
PartLocatorOptions
,这里是ManufacturerOptions表外键的引用。
如果不想保存在数据库中,可以序列化成JSON格式,可以在文件中,也可以在云服务上,也可以在数据库中。
每当我考虑影响多行——同一个表或跨多个表——我开始考虑使用存储过程,这样我就可以对数据库进行一次调用(效率)并且整个事务可以服从 SQL ACID。