I'm having a hard time understanding the repository pattern.
There are a lot of examples in the web, but most of them are using some kind framework like EF based examples, that's very confusing to start with.
One thing that I'm not able to understand, what most tutorial suggested, is that a repository should act like a in-memory database, i.e, they should have add()
, remove()
, find()
etc methods. But if I'm not using any persistence framework like Hibernate
, where should I put the logic to save these objects to database? Should there be a separate data access layer?
Another thing that is confusing me a lot is what happens when a object contains reference to another object.
For example, a Customer
can have many Address
and there is an identifying relationship between Customer
and Address
public class Customer {
private String firstName;
private String lastName;
private List<Address> addresses; // one or more addresses
// ...
}
public class Address {
protected String street;
protected String city;
// ...
}
Should there be two separate repositories for Customer
and Address
?
How should such a repository structure look like if there is a lookup table involved (e.g. A Book
having an Owner
, in a non-identifying relationship) ?
I hope somebody gives an explanation so that I finally get my head around this.
Aucun commentaire:
Enregistrer un commentaire