deamon
deamon

Reputation: 92539

Pros and cons of DDD Repositories

Pros:

Cons:

Questions:

Upvotes: 12

Views: 5019

Answers (3)

Dmitry
Dmitry

Reputation: 17350

Repository brings domain model into focus by hiding data access details behind an interface that is based on ubiquitous language. When designing repository you concentrate on domain concepts, not on data access. From the DDD perspective, using ORM API directly is equivalent to using SQL directly.

This is how repository may look like in the order processing application:

List<Order> myOrders = Orders.FindPending()

Note that there are no data access terms like 'Criteria' or 'Query'. Internally 'FindPending' method may be implemented using Hibernate Criteria or HQL but this has nothing to do with DDD.

Method explosion is a valid concern. For example you may end up with multiple methods like:

    Orders.FindPending()
    Orders.FindPendingByDate(DateTime from, DateTime to)
    Orders.FindPendingByAmount(Money amount)
    Orders.FindShipped()
    Orders.FindShippedOn(DateTime shippedDate)
    etc

This can improved by using Specification pattern. For example you can have a class

class PendingOrderSpecification{
    PendingOrderSpecification WithAmount(Money amount);
    PendingOrderSpecification WithDate(DateTime from, DateTime to)
    ...
}

So that repository will look like this:

Orders.FindSatisfying(PendingOrderSpecification pendingSpec)
Orders.FindSatisfying(ShippedOrderSpecification shippedSpec)

Another option is to have separate repository for Pending and Shipped orders.

Upvotes: 8

Justin Bozonier
Justin Bozonier

Reputation: 7712

The main point of a repository (as in Single Responsibility Principle) is to abstract the concept of getting objects that have identity. As I've become more comfortable with DDD, I haven't found it useful to think about repositories as being mainly focused on data persistence but instead as factories that instantiate objects and persist their identity.

When you're using an ORM you should be using their API in as limited a way as possible, giving yourself a facade perhaps that is domain specific. So regardless your domain would still just see a repository. The fact that it has an ORM on the other side is an "implementation detail".

Upvotes: 8

Robert Harvey
Robert Harvey

Reputation: 180878

A repository is really just a layer of abstraction, like an interface. You use it when you want to decouple your data persistence implementation (i.e. your database).

I suppose if you don't want to decouple your DAL, then you don't need a repository. But there are many benefits to doing so, such as testability.

Regarding the combinatorial explosion of "Find" methods: in .NET you can return an IQueryable instead of an IEnumerable, and allow the calling client to run a Linq query on it, instead of using a Find method. This provides flexibility for the client, but sacrifices the ability to provide a well-defined, testable interface. Essentially, you trade off one set of benefits for another.

Upvotes: 7

Related Questions