I have been reading about what people said regards repositories location (in which layer) in DDD context and found things that don't feel right to me. Example:

> 
> Is true that "in terms of more "classical" DDD, yes domain objects are
>  typically not allowed anywhere outside of the domain."
> 
> 
> 

from [here](https://stackoverflow.com/a/5897440/1785721)

Can any body point me to some other reference that state something like that? or at least an explanation why?

What feel right to me is that *some* repositories belong to Domain Layer. As Evans said, repositories must *mainly* used to return Aggregates to avoid:

> 
> ...breach the encapsulation of domain objects and AGGREGATES...
> 
> 
> 

and follow:

> 
> ...A REPOSITORY lifts a huge burden from the client, which can now talk
>  to a simple, intention-revealing interface, and ask for what it needs
>  in terms of the model...
> 
> 
> 

so **if Aggregates is a Domain Object** and is returned by *some* repositories that lead us to have repositories that must know how to reconstruct those Domain Objects, such repositories implementations will have a very close relationship with others parts of Domain Layer like simple Aggregates class definition or reconstruction factories.

These thoughts lead me to the second question, is habitual that App Layer retrieve Domain Object from the repositories outside a *Naked Object* context? I feel that yes, and that the use of Data Transfer Objects is just needed when performance or another specific reason justified it, but if we design the Domain Layer interface in a way that knowledge leak is avoided (just exposing Domain Objects and Domain Services needed to the App Layer and not the internal ones), we will be safe. Has sense that line of thought?

I said *some* in the previous paragraphs cos I think that some repositories could be not so tied to the Domain Layer, I talking about repositories to access some kind of values or enumerable objects or in general, objects that are no so tied to the Domain. Evans talk about too, he considered that some times have sense a global search, and that such global search will not get any harm to the a design.

Another reason to the existence of repositories is avoid:

> 
> Exposure of technical infrastructure and database access...
> 
> 
> 

and 

> 
> decouple application and domain design from persistence
>  technology, multiple database strategies, or even multiple data
>  sources.
> 
> 
> 

This other goals of the repository pattern let me to think at first that repositories can't be on the Domain Layer, that is contradictory with what I already said.

At the end, I think that exist two type of repositories if we classified them accord to its layer location:

1-One that belong to Infrastructure Layer witch an interface returning non Domain Layer objects, avoiding that lowest layers depend on highest layers. This kind of repositories will be used by Application Layer mostly to retrieve some kind of VALUES/ENUMERABLE objects.

2- The other type of repositories return Domain Objects and reside into the Domain Layer. This type of repositories depend of interfaces provided by the Infrastructure Layer for basically *decouple application and domain design from persistence technology* while allow Application Layer talk with the domain in an *intention-revealing interface, and ask for what it needs in terms of the model*. These kind of interfaces provided by the Infrastructure could be expressed in terms simple and raw data contracts and could be seem like a second form of repository through which Domain Layer ask the data (to the Infrastructure Layer) it need to form a Domain Object and reply a request from the Application Layer. Seem to me that in practice I can end with *duplicate code* in cases where Domain Object are very simple and similar in form to how they are stored in database. I will appreciate any comment about these problems and how the code is organized to solve them in the context of DDD.

I will thank critics to these thoughts, mainly those whose highlight its soft points ;)

Here a diagram of what I'm talking about

![enter image description here](https://i.stack.imgur.com/p2u0c.png)

**EDIT:**

The more I read about this, the more I think that this distinction is very important and could get some understanding to the community. For example what is described [here](http://martinfowler.com/eaaCatalog/repository.html)is Infrastructure Repositories (type 1 in what I wrote) while [here](http://martinfowler.com/eaaCatalog/repository.html) describe Domain Repositories (type 2 in what I wrote).

Even more, I think that part of the fight like [this one](http://ayende.com/blog/3973/night-of-the-living-repositories) are promoted for the lack of the distinction I made here.