【发布时间】:2013-02-18 13:26:51
【问题描述】:
我正在开发一个 ASP.NET MVC C# 应用程序,其中 MVC 应用程序作为 3 轮胎应用程序中的 UI 层,EF CodeFirst 5 作为 DAL。
DAL -> DTL
DTL <- BLL -> DAL
DTL <- UI -> BLL
箭头表示用途。 (所以 DAL 使用 DTL 等等...)
我使用 POCO 作为数据传输对象,并根据 EF Code First 进行数据相关验证。 我的 POCO 有关系,而关系中的 POCO 也有关系。很标准...
BL 中的所有类都根据单一职责规则处理单一 POCO 类型。 令我困惑的事情是,当 POCO 的关系是其他 BL 关注的(以保持单一责任)时,我如何在 POCO 上应用业务规则和验证?
我将尝试给出一个简化的示例:(请将此作为伪代码阅读)
public class Customer{
public int Id { get; set; }
public string Name { get; set; }
public virtual Enumerable<Person> Users { get; set; }
}
public class Person{
public int Id { get; set; }
public string Name { get; set; }
public Customer BelongsTo { get; set; }
public virtual Enumerable<Property> Properties { get; set; }
}
public class Property{
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class CustomerBL{
private DALContext = new DALContext();
public void Add(Customer customer){
DALContext.Customers.Add(customer);
DALContext.SaveChanges();
}
}
public class PersonBL{
private DALContext = new DALContext();
public void Add(Person person){
if(person.Properties.Count() < 3)
throw new ApplicationException("Each person must have at least three properties");
DALContext.People.Add(person);
DALContext.SaveChanges();
}
}
所以我的问题是,在添加新客户时,如何确保所有用户(如果在客户对象上设置了任何用户)都按照 PersonBL 的 Add 方法中的说明进行验证,而不会破坏单一责任规则 (或者至少以一种好的方式打破它)?
请记住,Person 可以拥有其他关系,这些关系具有其他关系,并且都具有特定的 BL 和业务规则。我提供了一个“特定”示例,但我正在寻找更通用的解决方案。谢谢!
【问题讨论】:
标签: .net validation architecture orm domain-driven-design