0

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?

  • 2
    Can this answer your question? [Repository Pattern and Joined Queries](https://softwareengineering.stackexchange.com/questions/375441/repository-pattern-and-joined-queries/375451#375451) – Fabio Oct 06 '21 at 18:55
  • I'm not totally sure. I see in that example they have a `GetWithLinesById` method, which would appear to be the same as my `FindHotelByIdWithRooms` example. 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:12
  • 1
    Upon second glance, I think you're right. The main part to focus on being `So, 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:23
  • To me this question is an indication of a model that hasn't been fully fleshed out. How is it that that a `Hotel` could both *have* and *not have* a collection of `Room`? 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

0 Answers0