复杂DTO的CRUD操作



需要在复杂的嵌套dto上实现Update和Delete操作的"最佳"方式的建议。举个简单的例子,假设我们有这样的结构:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Employer Company { get; set; }
}
public class Employer
{
    public string Name { get; set; }
    public string Address1 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

此处根据Employer更新Person可能意味着以下几点:1. 以前没有雇主的人,我们需要做插入到DB介绍新的雇主。2. 以前有一个雇主,我们只是更新雇主的内部数据3.雇主已从Person中删除

问题:

如果你有一个域/业务组件对象,比如PersonBusinessComponent和一些方法,比如PersonBusinessComponent. update (Person)确定正在执行的场景并应用更改的最佳方法是什么?这意味着如果它是一个删除操作,那么我们将调用一些EmployerDALC。Delete方法,如果是Insert,那么显然是EmployerDALC。插入等等……我理解一个选项是从Database获取当前版本,然后冗长地比较Person中每个嵌套对象的存在,但我希望有一些更好的方法,甚至可能是更通用的方法,可以实现在整个解决方案中处理任何此类操作。

注意:我没有使用MS实体框架。

这取决于系统的体系结构。这是一个过程模型,一个ActiveRecord模型还是一个域模型?我看到你在使用dto,所以这意味着一个领域模型。

如果是这样,那么您的业务逻辑(在'Services'层内)将负责编排操作,例如:

public interface PersonManager
{
  void CreateNewPerson(Person person);
  void DeletePerson(Person person);
  void ModifyPerson(Person person);
  // ... and so on .../
}

PersonManager将负责检查对象,并根据所运行的方法确定如何处理它。

然后,它将推迟到自己的业务逻辑层(可以与DAL交谈),以确定应该如何实现。例如,使用Modify方法,它可以查询DAL以获取该人员的当前雇主,如果雇主已经更改,则推迟到ModifyEmployer等:

public void ModifyPerson(Person person)
{
  var currentEmployer = DAL.Employers.Get(Person.Employer.EmployerID);
  if (currentEmployer != person.Employer)
  {
    // Try and get a matching Employer from the appropriate Service (liaising with the DAL)
    var employer = EmployerManager.GetEmployer(person.Employer.EmployerID);
    if (employer == null)
    {
      // ... Create a new employer
    }
    else if (employer != person.Employer)
    {
      // ... Update existing employer
    }
  }
  // ... Now go ahead and handle any changes to the person
}

在我的脑海里,我想不出任何特殊的包来为你处理这个问题,一般来说,我想说这都是在你的系统架构和BL如何与DAL交谈,但我相信这里的一个大脑盒子会想出一些更好的建议:)

希望这可能会有所帮助!

k .

最新更新