2017-03-18 76 views
0

使用指定模式是否正確使用導航屬性?帶導航屬性的指定模式

我有如下方面:

當我添加一個學生,我需要驗證的地址。

學生類:

public class Student { 
    public string Name { get; set; } 
    public DateTime Birth { get; set; } 
    //... 
    public virtual ICollection<StudentAddress> StudentAdresses { get; set; } 
} 

StudentAddress類:

public class StudentAdress{ 
    public int Id { get; set;} 
    public string Street { get; set; } 
    //... 
} 

在我的學生服務(DDD):

服務:

public void AddStudent(Student student) 
{ 
    // code 
    var studentValidation = new StudentValidation().Validate(student); // Student Validation has a set of specifications that will populate a validation result object and that I'll retrieve it by Domain Controller Notification (MVC) 
    // code 
} 

PS:學生驗證了一套規範,將填充驗證結果對象,我會通過域控制器通知(MVC)

回到問題檢索...

我可以在哪裏放置我的學生地址班級規格?

我想到了將它們放入StudentValidation類中的可能性,並且使用Navigation屬性可以驗證每個地址。我不知道這是否正確。這將是一種橫向驗證。

+0

爲什麼StudentAddress是一個實體?它應該是一個價值對象。 –

+0

我認爲我對這個問題的回答可能會指導你在正確的方向關於UI驗證與域驗證:http://stackoverflow.com/questions/28395176/should-i-abstract-the-validation-framework-from-domain-圖層/ 28397201#28397201 – plalx

+0

Constantin,StudentAddress是我DB上的表格。它具有身份,我認爲價值對象是特定實體的一組屬性。 –

回答

0

在DDD中,驗證是一種確保滿足不變量的形式。這是聚合中的聚合根的責任。在你的例子中,也許Student是Student Aggregate的根,StudentAddress是一個孩子。在這種情況下,學生有責任確保聚合處於有效狀態。理想情況下,這個邏輯應該真實地存在於Student類本身中,但在你的情況下,你似乎使用StudentService來執行Student的驗證。因此,就你的情況而言,從StudentService執行地址驗證是很好和適當的(IMO),因爲它基本上承擔了你的聚合根的責任。

您是否需要單獨的StudentAddress校驗器類型而不是Student的版本取決於相關驗證規則的上下文/範圍。例如,如果您必須驗證地址是否包含城市,則很容易在StudentAddress級別完成。但是,如果您需要驗證學生是否至少有一個地址,或者學生沒有兩個重複地址,則需要在學生級完成。

您可以通過以下方式瞭解更多關於DDD的信息here