DDD应用中如何持久化表现层专属UI配置?避免领域与基础设施耦合
DDD架构下表现层UI配置持久化的解决方案探讨
我有一个采用Domain、Application、Presentation三层架构的DDD应用。表现层需要持久化一些与业务无关、仅关联UI元素行为的配置。由于这些配置绑定到特定表现层基础设施,我不愿在应用层引入专属服务;但单独为表现层组件构建持久化基础设施既不合理,又会产生大量冗余工作——应用层已具备完整的数据访问和仓储基础代码。这是常见需求,例如前端可能需要保存用户偏好,而应用本身并不关注用户。
请问是否有相关文献、模式或文章可以提供解决方案?
具体场景
该应用是基于.NET框架、使用EF(Entity Framework)、仓储模式,以WPF作为前端的应用。应用可自主运行扫描任务并协调各类设备,前端用于查看扫描结果。应用支持为已完成的扫描绑定“自定义属性”(简单的键值字符串对)。
针对这些“自定义属性”的UI控件有时需要填充默认值,有时则不需要。UI需要知晓每个CustomProperty是否应启用该行为,本质是为每种属性存储配置。这不属于“用户偏好”,也并非所有前端都需要的功能。
现有领域实体
public class CustomPropertySpec : Entity { public CustomPropertySpec(string name, string defaultValue, bool hasPresets, bool isValidating, string validationRegex) { this.Name = name ?? throw new ArgumentNullException(nameof(name)); this.DefaultValue = defaultValue; this.HasPresets = hasPresets; this.IsValidating = isValidating; this.ValidationRegex = validationRegex ?? throw new ArgumentNullException(nameof(validationRegex)); this.Presets = new HashSet<CustomPropertySpecPreset>(); this.Localizations = new HashSet<CustomPropertySpecLocalization>(); } /// <summary> /// Initializes a new instance of the <see cref="CustomPropertySpec"/> class. /// </summary> protected CustomPropertySpec() { this.Presets = new HashSet<CustomPropertySpecPreset>(); this.Localizations = new HashSet<CustomPropertySpecLocalization>(); } public string Name { get; set; } public string DefaultValue { get; set; } public ICollection<CustomPropertySpecPreset> Presets { get; set; } public ICollection<CustomPropertySpecLocalization> Localizations { get; set; } public bool HasPresets { get; set; } public bool IsValidating { get; set; } public string ValidationRegex { get; set; } public bool Validate(string value) { if (!this.IsValidating) { return true; } else { Regex regex = new Regex(this.ValidationRegex, RegexOptions.IgnoreCase); return regex.IsMatch(value); } } }
我可以直接在此实体中添加几个布尔字段来满足前端的配置需求,但这会导致领域层与特定基础设施耦合。
内容的提问来源于stack exchange,提问作者Andrea Bardelli
相关产品推荐
相关产品推荐

