2017-03-18 83 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