title: Repository Pattern - How to understand it and how does it work with "complex" entities?
body: <p>I'm having a hard time understanding the Repository Pattern.</p>
  
  <p>There are a lot of opinions on that topic like in <a href="http://www.codeproject.com/Articles/526874/Repositorypluspattern-cplusdoneplusright">Repository pattern done right</a> but also other stuff like <a href="http://ayende.com/blog/3955/repository-is-the-new-singleton">Repository is the new Singleton</a> or again like in <a href="http://thinkinginobjects.com/2012/08/26/dont-use-dao-use-repository/">Don't use DAO use Repository</a> or just take <a href="http://fruzenshtein.com/spring-jpa-data-hibernate-mysql/">Spring JPA Data + Hibernate + MySQL + MAVEN</a> where somehow a Repository appears to be the same as a DAO object.</p>
  
  <p>I'm getting tired of reading this stuff since imho this can't be such a hard thing as it is displayed in a lot of articles.</p>
  
  <p>I see it like this: It appears that what I want is something like this:</p>
  
  <pre><code>         ------------------------------------------------------------------------
           |                            Server                                    |
           ------------------------------------------------------------------------
           |                    |                        |                        |
  Client &lt;-|-&gt; Service Layer  &lt;-|-&gt;  Repository Layer  &lt;-|-&gt; ORM / Database Layer |
           |                    |                        |                        |  
           ------------------------------------------------------------------------
  </code></pre>
  
  <p>The <code>Service Layer</code> takes <code>*DTO</code>objects and passes those to the <code>Repository Layer</code> that basically is nothing more than "the guy" who knows <em>how</em> an entity can be stored.</p>
  
  <p>For example assume you have a composition of some tools (<em>please note that this is just pseudo code</em>)</p>
  
  <pre><code>@Entity
  class ToolSet {
    @Id
    public Long id;
    @OneToOne
    public Tool tool1;
    @OneToOne
    public Tool tool2;
  }
  
  @Entity
  class Tool {
    @Id
    public Long id;
    @OneToMany
    public ToolDescription toolDescription;
  }
  
  @Entity
  class ToolDescription {
    @Id
    public Long id;
    @NotNull
    @OneToOne
    public Language language
  
    public String name;
    public String details;
  }
  </code></pre>
  
  <p>The thing I'm not getting is the part where I am getting a <code>ToolSetDTO</code> object from the client.</p>
  
  <p>As I understood it so far I could write a <code>ToolSetRepository</code> with a method <code>ToolSetRepository.save(ToolSetDTO toolSetDto)</code> that "<em>knows how to store</em>" a <code>ToolSetDTO</code>. But almost every tutorial does not pass the <code>*DTO</code> but the <code>Entity</code> instead.</p>
  
  <p>What's bothering me here is that if you take my <code>ToolSet</code> example from above I'd have to do the following steps:</p>
  
  <ol>
  <li>Take <code>toolSetDto</code> and check if not <code>null</code></li>
  <li>For each <code>tool*Dto</code> owned by <code>toolSetDto</code> <br/>a) If has a valid id then convert from <code>DTO</code> to <code>Entity</code> otherwise create a new database entry <br/> b) <code>toolDescriptionDto</code> and convert/save it to the database or create a new entry</li>
  <li>After checking those above instanciate <code>ToolSet</code> (entity) and set it up for persisting it in the database</li>
  </ol>
  
  <p>All this is too complex to simply let the service function (interface for the client) handle this. </p>
  
  <p>What I was thinking about was creating e.g. a <code>ToolSetRepository</code> but the question here is</p>
  
  <ul>
  <li>Does it take a <code>ToolSet</code> entity object or does it use a <code>DTO</code> object?</li>
  <li>In any case: Is the <code>*Repository</code> allowed to <em>use</em> other repository objects? Like when I want to save <code>ToolSet</code> but I have to store <code>Tool</code> and <code>ToolDescription</code> first - would I use <code>ToolRepository</code> and <code>ToolDescriptionRepository</code> inside <code>ToolSetRepository</code>? <br/> If so: Why doesn't it break the Repository Pattern? If this pattern is basically a layer between the service and my ORM framework it just does not "feel right" to add dependencies to other <code>*Repository</code> classes due to dependency reasons. </li>
  </ul>
  
  <p>I don't know why I can't get my head around this. It does not sound <em>that</em> complicated but there's still help out there like <code>Spring Data</code>. Another thing that is bothering me since I really don't see how this makes <em>anything</em> easier. Especially since I'm using Hibernate already - I don't see the benefit (but maybe that's another question).</p>
  
  <p>So .. I know this is a long question but I put already a few days of research into it. There's already existing code I am working on right now that starts to become a mess because I just can't see through this pattern.</p>
  
  <p>I hope somebody can give me a bigger picture than most of the articles and tutorials which do not get beyond implementing a very, very simple example of a Repository Pattern.</p>