I'm having a hard time understanding the Repository Pattern.
There are a lot of opinions on that topic like in Repository pattern done right but also other stuff like Repository is the new Singleton or again like in Don't use DAO use Repository or just take Spring JPA Data + Hibernate + MySQL + MAVEN where somehow a Repository appears to be the same as a DAO object.
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.
I see it like this: It appears that what I want is something like this:
------------------------------------------------------------------------
| Server |
------------------------------------------------------------------------
| | | |
Client <-|-> Service Layer <-|-> Repository Layer <-|-> ORM / Database Layer |
| | | |
------------------------------------------------------------------------
The Service Layer
takes *DTO
objects and passes those to the Repository Layer
that basically is nothing more than "the guy" who knows how an entity can be stored.
For example assume you have a composition of some tools (please note that this is just pseudo 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;
}
The thing I'm not getting is the part where I am getting a ToolSetDTO
object from the client.
As I understood it so far I could write a ToolSetRepository
with a method ToolSetRepository.save(ToolSetDTO toolSetDto)
that "knows how to store" a ToolSetDTO
. But almost every tutorial does not pass the *DTO
but the Entity
instead.
What's bothering me here is that if you take my ToolSet
example from above I'd have to do the following steps:
- Take
toolSetDto
and check if notnull
- For each
tool*Dto
owned bytoolSetDto
a) If has a valid id then convert fromDTO
toEntity
otherwise create a new database entry
b)toolDescriptionDto
and convert/save it to the database or create a new entry - After checking those above instanciate
ToolSet
(entity) and set it up for persisting it in the database
All this is too complex to simply let the service function (interface for the client) handle this.
What I was thinking about was creating e.g. a ToolSetRepository
but the question here is
- Does it take a
ToolSet
entity object or does it use aDTO
object? - In any case: Is the
*Repository
allowed to use other repository objects? Like when I want to saveToolSet
but I have to storeTool
andToolDescription
first - would I useToolRepository
andToolDescriptionRepository
insideToolSetRepository
?
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*Repository
classes due to dependency reasons.
I don't know why I can't get my head around this. It does not sound that complicated but there's still help out there like Spring Data
. Another thing that is bothering me since I really don't see how this makes anything easier. Especially since I'm using Hibernate already - I don't see the benefit (but maybe that's another question).
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.
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.