DDD资料库除了可以使用“真实”内容外,还可以使用摘要对象吗?对象 [英] Is it ok for a DDD repository work with summary objects in addtion to "real" objects

查看:95
本文介绍了DDD资料库除了可以使用“真实”内容外,还可以使用摘要对象吗?对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我正在创建一个存储库,以存储数字电子书,如下面的界面所示。该存储库将存储书籍的实际文本以及标识书籍的元数据(标题,作者,出版商,ISBN等。)。

Say I'm creating a repository to store digital E-Books as shown in the interface below. This repository will store the actual text of the book, as well as the metadata that identifies the book (title, author, publisher, ISBN etc..).

public interface IBookRepository
{
    void AddBook(Book newBook);
    void DeleteBook(int bookId);
    void UpdateBook(Book updatedBook);
    Book GetBook(int bookID)
} 

public class Book
{
    public int BookId {get; set;}
    public string Title {get; set;}
    public string Author {get; set;}
    public IList<Page> Contents {get; set}
}

public class Page
{
    public int PageNumber {get; set;}
    public string PageContent {get; set;}
}

在大多数情况下,我不希望检索其全文这本书,因为那将是相当昂贵的。在大多数情况下,我只关心元数据,例如,我可能只想创建一本书清单。那么就DDD而言,允许 IBookRepository 具有返回 BookSummary 对象的方法是否可以接受?书籍摘要对象将包括元数据,但不包括书籍的实际内容。

In most cases I would not want to retrieve the entire text for the book, as that would be rather expensive. In most cases all I care about is the metadata, for example I may simply want to create a list of books. So would it be acceptable in regards to DDD to also allow an IBookRepository to have methods that return BookSummary objects? Book summary objects would include the metadata but not the actual contents of the book.

使用 UpdateBook(BookSummary book)方法怎么样?假设我想更新 Book.Rating 属性,但是不需要/想要从存储库中读取本书的全部内容。

What about having an UpdateBook(BookSummary book) method? Say I want to update the Book.Rating property, but don't need/want to read the entire contents of the book from the repository to do this.

public interface IBookRepository
{
    //Full Book Methods
    void AddBook(Book newBook);
    void DeleteBook(int bookId);
    void UpdateBook(Book updatedBook);
    Book GetBook(int bookID)

    //BookSummary Methods
    BookSummary GetBookSummary(int bookID)
    IEnumerable<BookSummary> GetBooksByAuthor(string authorName);
    IEnumerable<BookSummary> GetBooksByGenre(int genreId);
    void UpdateBook(BookSummary bookSummary);
} 


public class BookSummary
{
    public int BookId {get; set;}
    public string Title {get; set;}
    public string Author {get; set;}
    public int PageCount {get; set;}
}

注意:我知道使用带有延迟加载的ORM也会是一个解决方案,但是我想在不使用延迟加载的情况下设计存储库

推荐答案

在DDD存储库中只能与聚合根一起使用。我想在您的情况下,BookSummary只是Book聚合内的一个实体(当然,这里需要进行更准确的分析),因此应该通过BookRepository获取Book,然后使用延迟加载从聚合根遍历到BookSummary。否则,您将不在此处应用域驱动设计

In DDD repository should work only with aggregate root. In your case, I suppose, BookSummary is just an entity inside of Book aggregate (of course, more accurate analysis is needed here), so should get Book via BookRepository and then traverse to BookSummary from aggregate root using lazy loading. Otherwise, you are not applying Domain Driven Design here

这篇关于DDD资料库除了可以使用“真实”内容外,还可以使用摘要对象吗?对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆