3

Can someone please explain the best way to solve this problem.

Suppose I have three classes

  1. Person

  2. Venue

  3. Vehicle

I have a DAO method that needs to return some or all of these attributes from each of the classes after doing a query.

Please note, by requirements I am using one DAO for all three classes and no frameworks. Only my own MVC implementation

How do I accomplish this? It seems very wrong to make a class PersonVenueVehicle and return that as an object to get the instance field, values.

I was taught that the database entities must be reflected by classes, if this is case how is it implemented in such a situation?

1
  • Use one object per query. Do not try to reuse some PersonVenueVehicle by multiple queries. It doesn't make any sense. Use interfaces with meaningful names if you need some of the query results to have something in common.
    – lorus
    CommentedApr 4, 2013 at 5:29

2 Answers 2

0

Start with methods that run a specific query and return just the data that you need, as opposed to a more complex method that can return many combinations of data.

I'm guessing there's some kind of relationship between the classes you mentioned, such as that a Person has one or more Vehicles, and maybe they commonly travel to one or more Venues, so model your classes to reflect those relationships. In this example, your Person object would contain a List<Vehicle> and a List<Venue>.

So if you need to retrieve one Person and all their associated data, create a DAO method to return a single Person object with its collections loaded.

If you just want the Person with their Vehicles, create a DAO method to return a Person object with their List<Vehicle> loaded and their List<Venue> null.

If you want a list of Persons, and you don't immediately need their Vehicles or Venues, return a List<Person> with null collections.

    0

    If you are retrieving an Object or a list of Objects, use one object per query on your controller. If you want to retrieve a message say in JSON or XML, then you DAO acts like a facade and can query several class attributes.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.