I'm currently working on refactoring a project to use the repository pattern, but I'm currently struggling with how related information should be retrieved.
Let's say I have a Hotels and Rooms. I have 3 use-cases that come up:
- Retrieve a hotel with all of its rooms
- Retrieve a hotel (without rooms)
- Retrieve a room
Now the last 2 seem simple enough to me, I would have some kind of findHotelById and findRoomById methods in my repositories. Now what is the best practice for retrieving both the hotel and the rooms? Should I have a findHotelByIdWithRooms method? Should I call findHotelById and then call findRoomsByHotel and populate the rooms on my model (ex: hotel.setRooms(rooms)) after the fact? Or should findHotelById take in some parameter to optionally include rooms in the result?
GetWithLinesByIdmethod, which would appear to be the same as myFindHotelByIdWithRoomsexample. Although I don't feel that I am attempting to mirror the database like mentioned in that issue, as Rooms and Hotels would be 2 separate entities – Matthew Weeks Oct 06 '21 at 19:12So, instead of mirroring database structure in your repository, have abstractions which fit domain needs and then implement those abstractions by effectively using database features.– Matthew Weeks Oct 06 '21 at 19:23Hotelcould both have and not have a collection ofRoom? What I'm saying is a "hotel with rooms" is a different entity than a "hotel without rooms". Your confusion is a result of trying to bridge this gap without updating your understanding of the model. – user3347715 Oct 09 '21 at 15:24