2009-11-05 49 views
1

我是DDD和存儲庫模式的新手,所以我對它的理解可能完全錯誤。但我正在努力學習它。話雖如此,我需要創建一個應用程序,它顯示了商店的區域。我爲此創建了一個ZoneRepository,這個工作到目前爲止只有我的幾個方法。現在在該應用程序中,我還需要顯示該商店的不同樣式。樣式列表將用於將它們拖入各個區域。現在我的問題是風格類屬於哪裏,因爲它是一種小型報告。那個「StyleReport」是否屬於版本庫?它是否屬於別的地方?你怎麼知道它屬於哪裏?請幫我理解。DDD與存儲庫模式和報告混淆

回答

3

存儲庫僅對聚合根位置起作用。聚集是圍繞一個或多個被視爲單位的對象的邊界。我的意思是,當你操作這些數據(插入,更新,刪除等)時,該邊界內的所有對象都會相應地受到影響。每個聚合都有一個根。這根是由軟件的其他部分在外部引用的。我想有一種描述它的方式是「不依賴別的東西」。

從現有模型的描述中推導出您的域的正確定義有點困難。此外,設計應基於業務模型和需求,而不是您的UI或應用程序的工作方式。所以,你應該對你正在解決的一般問題進行建模,而不是你想如何解決它。

這聽起來像你有一個實體商店。商店可以分爲一個或多個區域。每個區域都有一個或多個StyleReports。這聽起來像區域依賴於商店,所以商店是聚合根。現在,也許這些StyleReport實體是您在問題域中提供的一組全局對象(這意味着您可以單獨定義StyleReports,在應用程序範圍內並在您的區域中引用它們)。在這種情況下,也許StyleReport也是一個聚合根。

以下是一些示例模型(C#,不知道您使用的是哪種語言)。但是,不要把這當成絕對的話。如果我不知道你的域名的具體情況,我不能很好地模擬它。

public class Store 
{ 
    public Int32 ID { get; } 
    public String Name { get; set; } 
    public IList<Zone> Zones { get; private set; } 

    public Store() 
    { 
     Zones = new List<Zone>(); 
    } 

    public void AddZone(Zone zone) 
    { 
     Zones.Add(zone); 
    } 
} 

public class Zone 
{ 
    public Int32 ID { get; } 
    public String Name { get; set; } 
    public IList<StyleReport> Styles { get; private set; } 

    public Zone() 
    { 
     Styles = new List<StyleReport>(); 
    } 

    public void AddStyle(StyleReport style) 
    { 
     Styles.Add(style); 
    } 
} 

public class StoreRepository : Repository<Store> 
{ 
    public Store Get(Int32 id) 
    { 
     // get store from persistence layer 
    } 

    // find, delete, save, update, etc. 
} 

public class StyleReportRepository : Repository<StyleReport> 
{ 
    public StyleReport Get(Int32 id) 
    { 
    // get style from persistence layer 
    } 

    // find, delete, save, update, etc. 
} 

所以修改時,商店的地區,增加款式,也許這樣的事情

IRepository<Store> storeRepository = new StoreRepository(); 
IRepository<StyleReport> stylesRepository = new StyleReportRepository(); 

Store store = storeRepository.Get(storeID); // store id selected from UI or whatever 

// add a zone to the store 
Zone someZone = new Zone { Name = zoneNamea }; // zone name was entered by the UI 
someZone.AddStyle(styleRepository.Get(styleID)); // style id was selected from UI 

storeRepository.Update(store); 
+0

感謝這麼多的詳細答覆。我很感激它。StyleReport依賴於商店而不是區域。因此我想我會將它移入StoreRepository。該報告僅返回當前商店+其他一些屬性的所有樣式。 – vikasde 2009-11-05 19:53:35

+0

我發現這個評論在雅虎組DDD板,可能會有所幫助:http://tech.groups.yahoo.com/group/domaindrivendesign/message/9713 我需要操作這個聚合直接在一些操作?是的,那麼它可能是一個根,可以通過存儲庫訪問。 希望這有助於! – HackedByChinese 2009-11-05 19:59:07

+0

是的,這確實非常有幫助。非常感謝。 – vikasde 2009-11-05 20:01:21