2011-12-23 61 views
1

需要對在複雜嵌套DTO上實現更新和刪除操作的「最佳」方法提供建議。對於非常簡單的例子,假設我們有這樣的結構:複雜DTO上的CRUD操作

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; } 
} 

到人的更新按照僱主在這裏可能意味着幾件事情:1。 以前沒有僱主的人,我們需要做的插入到DB來引進新的僱主。 2.以前僱主,我們剛剛更新了用人單位的內部數據 3.僱主已經從人取走

問:

如果你有一個域/業務構成元素對象像PersonBusinessComponent一些方法如PersonBusinessComponent.Update(Person) 什麼是最好的方式來確定哪個場景正在執行和應用更改 - 意味着如果它是一個刪除操作,那麼我們將調用一些EmployerDALC.Delete方法,或者如果它是一個Insert然後顯然EmployerDALC.Insert等... 據我所知,一種選擇是從數據庫中獲取當前版本,然後單調乏味地比較存在Person中的每個嵌套對象,但我希望有一些更好的方法,或者甚至可能更通用的方法來實現,以處理整個解決方案中的任何此類操作。

注意:我沒有使用MS實體框架。

+0

這取決於您的系統的體系結構。這是一個程序模型,一個ActiveRecord模型還是一個領域模型?我看到你正在使用DTO,這意味着一個域模型。 – 2011-12-23 06:39:48

回答

0

這取決於您的系統的體系結構。這是一個程序模型,一個ActiveRecord模型還是一個領域模型?我看到你正在使用DTO,這意味着一個域模型。

如果是的話那麼你的業務邏輯(「服務」層內)將負責策劃的操作,例如:

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

的的PersonManager然後將負責審查對象和工作什麼基於方法運行來處理它。

然後,它將推遲到它自己的業務邏輯層(可以與DAL交談)來確定應該如何實現。例如與修改方法就可以查詢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.