2009-11-05 54 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