#Repo patern in java
1 messages · Page 1 of 1 (latest)
@prime osprey , here is the thread
So I got a list of services, but each service has it's own repository
so let's use a simple example, let's say I got a class House, and a list of furniture
Are you using any ORMs like Hibernate?
No, frameworks
Are we speaking of the JPA specifications?
not sure what you talk about tbh
The Java Persistence API. Its a specification for exactly this.
No I don't use any of that
To me it wouldn't make much sense to do the mapping in the service, but also not in the Repo
Alright. Every entity has an ID.
If you have a Hous which has its own table and
Furniture which has its own table, then you would
map it by using a collection of IDs.
If your ID is an int then a Collection<Integer> or if its a UUID then
a Collection<UUID>. Just a more concrete implementation like List or Set.
Yes I understand, but ideally, you end up with both a database identifier like an id, and eventually, the goal would be that you end up with linked objects no?
So that I can do
House house = new House();
Door door = house.getDoor();
door.open();
Maybe a stupid example
so I would have 2 different repos, one for the house, and one for the door
when and where would I map these together
so maybe a specific code example:
What do you mean by "map these together"
You have 2 repositories. One for House and one for Door.
The House does not contain a Door but just an ID for a door.
When you call getDoor() then you fetch the door from the other repository.
There are 2 ways of fetching:
Lazy = first time getDoor() is called you retrieve the Door object
Eager = the door is loaded when then House is loaded
Usually you would do all of this using the Proxy pattern. (If you would use Hibernate or another ORM)
I will write a code example
brb 10min
public class A{
House house = this.houseService.getHouse("myhouse");
Door door = house.getDoor(); <-- this would not be possible then. It would be the ID of the house instead
}
alright, take ur time
Lets ignore that this isnt valid java.
getDoor() would call the door repositroy (or cache) and return a Door
Thats how you would do it with relation mapping
yea but that would not be possible
unless I am wrong
but the Door object is a model
and the model should not have dependency on Services
I made this small example drawing
the model should never interact with the repo
only the other way around
@prime osprey , does this example help?
so getDoor() should no have any service calls
ill show you what i mean later. Thats why orms use a proxy layer
so then the question is, where would I map together the 2, because when I call the house service, to fetch my house, I expect it comes with a door
If you have a n to n relationship then you dont receive a House with a Door
You receive a House with a Door-id. If you want to interact with the door through the House
object then you would need an aggregator or a facade.
So something like this?
class HouseService {
private final DoorService doorService;
public HouseService(DoorService doorService){
this.doorService = doorService;
}
House house = new House();
Door door = this.doorService.getDoorById(house.getDoorID());
}
The model does not need to be exposed to the client. You can have a business Entity which
can interact with multiple repositories
I know it's not valid java
This would lead to circular dependencies
In 1 to 1 (bidirectional) relations
But not if I use the ID's right?
The services build the circular dependency
so what you are saying, is instead of having a getter getDoor() and fetch a door object. I fetch the door object itself based on the doorId in the house?
What if Door has a field to get the House it is owned by?
Yes indeed
But if I would fetch the objects each seperate, then that would not be an issue anymore
I see what you are saying now
So basically, I don't return an object that has an actual reference relationship
I return the id's, and fetch the objects based on the ID through the approriate service
Thats why you would use an aggregator that has both the HouseService and DoorService.
Yes
Not quite sure what you mean with "Aggregator" I'll google it