5

Is it possible for an Aggregate root entity to have a method in which it will call a Repository?

I know it should not but want to get confirmed as Eric's book is also not saying anything clearly :(

one more thing where can I get a unit testing example for domain-driven design?

sajadre
  • 1,141
  • 2
  • 15
  • 30
batwadi
  • 1,836
  • 7
  • 29
  • 42
  • I don't mean to be dense or anything, but are we supposed to know who Eric is? – John Lockwood Oct 18 '09 at 07:14
  • 14
    Yeah, DDD + Eric = Eric Evans, which everyone knows. – alexn Oct 18 '09 at 14:38
  • See also: [DDD - the rule that Entities can't access Repositories directly](http://stackoverflow.com/questions/5694241/ddd-the-rule-that-entities-cant-access-repositories-directly) – codeulike May 05 '11 at 17:07

3 Answers3

4

This is a bit of a religious question. Some see no problem with this, while others might believe it is heresy to do so.

While I've normally always kept my Repository away from my Domain Model (and had an upstream service object deal with the Repository), I have had a project that "required" having the Repository accessable from the Domain Model. This was due to the Domain object needing to retrieve specific data based on business logic => using specification objects/Linq to nHibernate (the responsibility and knowledge of how to filter data belonged to that Domain Object) and/or performance reasons.

A caveat around doing this is how to get the reference to the Repository to the Domain object - in that case I utilized Constructor injection with an IOC tool.

Whether you should do this or not really depends on your solution/use case/technologies being used etc...

saret
  • 2,217
  • 13
  • 12
3

Can? -Yes.

Should? -No.

All answers are however context-sensitive, and you don't provide yours.

A generic advise would be to look for a "service" or "specification" type.

Martin R-L
  • 4,039
  • 3
  • 28
  • 28
  • Thanks Martin for your reply I also think Enities should not call repository but in one book auther has given example in which he calls Repository from the Person Entity, The book is "Patterns of Enterprise Application Architecture" by Martin Fowler. Please advice – batwadi Oct 19 '09 at 13:47
1

Behavior IS-WHAT-IT-IS. Eric calls a Repository like utility from a Brokerage Account entity called, "QueryService". He mentions that it's not a good design for a real project. So what do you do?

public class Clerk
{
    public Clerk()
    {
    }

    //Store Report in Database
    public void File(Report);
    {
        ReportRepository.Add(Report);
    }
}

You could do that, but it's probably best to tell the Clerk which Repository to use.

public class Clerk
{
    private IReportRepository _reportRepository;

    public Clerk(IReportRepository ReportRepository)
    {
        this._reportRepository = ReportRepository
    }

    //Store Report in Database
    public void File(Report);
    {
        this._reportRepository.Add(Report);
    }
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Roy Oliver
  • 338
  • 3
  • 10