Can someone please explain the best way to solve this problem.
Suppose I have three classes
Person
Venue
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?
-
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– lorus2013年04月04日 05:29:46 +00:00Commented Apr 4, 2013 at 5:29
2 Answers 2
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 Vehicle
s, and maybe they commonly travel to one or more Venue
s, 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 Vehicle
s, 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 Person
s, and you don't immediately need their Vehicle
s or Venue
s, return a List<Person>
with null collections.
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.
Explore related questions
See similar questions with these tags.