Data Gateway Pattern and Foreign Keys

312 Views Asked by At

How are this two concepts work together ?

I have a scenario

  • city table
  • country table
  • city.country_id is a FK to country.id

    Objective

  • fetch all the cities and display the country name also

    My problem

  • the fetch method will get the cities from the table
  • if I need the country name I would have to do an extra search for it or an inner join but by doing so I make extra queries when they are not necessary (display just the a city info for example)

    Question

  • What is the right way to apply the Data Gateway Pattern in this case.

  • 2

    There are 2 best solutions below

    0
    On BEST ANSWER

    You should use a join if you need h associated country name. Simply add another method like fetchWithCountry.

    0
    On

    Another option is to create a view of the city table that includes the CountryName. Then get your DataGateway to select using the view and save using the table. In your domain objects make the CountryName field a dumb read only field. This approach might seem like a dirty hack but it does simplfy things.