簡體   English   中英

使用域驅動設計為多對多關系建模

[英]Modelling a many-to-many relationship using Domain Driven Design

我正在嘗試使用域驅動的設計對多對多關系進行建模。

我的模型中的方案具有零個或多個父方案以及零個或模式子方案。 一對方案之間的關系描述如下:

ParentScheme,ChildScheme,RelationshipRule,RelationshipPriority

我正在考慮使用以下Scheme實體對此建模:

public class Scheme 
{
    public int Id {get; set;}
    public string SchemeName {get; set;}
    public IEnumerable<Scheme> Parents {get; set;}
    public IEnumerable<Scheme> Children {get; set;}
}

上述實體的問題在於它沒有捕獲RelationshipRule和RelationshipPriority值。 所以我正在考慮創建一個單獨的名為SchemeRelationship的實體

public class SchemeRelationship 
{
    public int Id {get; set;}
    public Scheme Scheme {get; set;}
    public Scheme ChildScheme {get; set;}
    public string RelationshipRule {get; set;}
    public int RelationshipPriority {get; set;}
}

並更改我的Scheme實體以使其成為:

public class Scheme 
{
    public int Id {get; set;}
    public string SchemeName {get; set;}
    public IEnumerable<SchemeRelationship> Parents {get; set;}
    public IEnumerable<SchemeRelationship> Children {get; set;}
}

這種方法的問題在於,在我的領域中,方案關系並不是真正的實體。 擁有id並沒有任何意義。 將關系建模為值對象也許更有意義? 我對使其成為值對象的唯一保留意見是認為值對象是不可變的,但在我們的領域中,RelationshipPriority可能會因關系而改變。 我是域驅動設計的新手,因此希望獲得有關如何最好地對其建模的任何建議。

如果“父母與子女”模式之間存在多對多關系,則以下設計可能對您的方法有用

class Schema
{
    public int Id { get; set; }
    public string SchemeName { get; set; }
    public string RelationshipRule { get; set; }
    public int RelationshipPriority { get; set; }
}

class ParentSchema : Schema
{
    public IEnumerable<ChildSchema> Children { get; set; }
}

class ChildSchema : Schema
{
    public IEnumerable<ParentSchema> Parents { get; set; }
}

--SJ

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM