In employing Domain Driven design I often encounter an issue regarding the various perspective on a domain object, especially when using NHibernate. Perspectives are essentially ways to view an domain object. For example, a simplified model:
class Transaction
{
string Id { get; set; }
string CustomerName { get; set; }
DateTime Date { get; set; }
decimal Amount { get; set; }
decimal Tax { get; set; }
}
class Batch
{
string Account { get; set; }
string Number { get; set; }
DateTime? ClearDate { get; set; }
IList<Transaction> Transactions { get; }
decimal Total { get; }
}
The total property on the batch is the sum of the amounts on each transaction. When considering a single batch this model works well and is a proper representation of the domain. The client application however has a screen which displays collections of batches. That screen does not require any details about the transactions within a batch, only the total amount. Utilizing the same object on the listing screen is slow. One option is to make the Total property be settable, another option is to create a new class, such as:
class BatchOverview
{
string Number { get; set; }
decimal Total { get; set; }
}
This would have its own repository and its own NHibernate mapping to a database view.
- Does this object belong to the domain model, or is it more application/GUI specific?
- Should the batch overview object reference the batch object?
- Is this a common pattern?
- Is there any guidance in relation to this issue?
DDD has the concept of bounded contexts, however in this case the context is the same. Both the Batch class and the BatchOverview class refer to the same 'actual' batch - they are different views, or perspectives on it.