tags:

views:

41

answers:

2

I have a Car class. It has three properties: id, color and model.

In a particular query I want to return all the cars and their properties, and I also want to return a true/false field called "searcherKnowsOwner" which is a field I calculate in my database query based on whether or not the individual conducting the search knows the owner. I have a database function that takes the ID of the searcher and the ID of the car and returns a boolean.

My car class looks like this (pseudocode):

class Car{
  int id;
  Color color;
  Model model;
}

I have a screen where I want to display all the cars, but I also want to display a flag next to each car if the person viewing the page knows the owner of that car.

Should I add a field to the Car class, a boolean searcherKnowsOwner? It's not a property of the car, but is actually a property of the user conducting the search. But this seems like the most efficient place to put this information.

+1  A: 

I would model it this way:

class Car{
  int id;
  Color color;
  Model model;
  Owner owner;
}

class Owner {
  Boolean knowsSearcher;
}

Now the Car type has an Owner (a bit backwards from real life, yes) and the Owner type has a field indicating whether or not they know the searcher.

Andrew Hare
I don't agree here. Owner object should not worry if searcher knows him.
this. __curious_geek
I also disagree. 'KnowsSearcher' only makes sense in the context of a search. It should not be part of the model. At most you could have a method such as Owner.KnowsPerson(Person person) that can be invoked in the context of a search, but to have a property that has a value without the context of the searcher doesn't make sense.this. __curious_geek has the right answer. It's always better to create seperate classes for search results anyway so that you can return aggregated data into a flat class without doing multiple queries to load all of the object's associations.
DavidMasters84
+3  A: 

Let the Car and Owner object remain unchanged. Define 2 classes SearchCriteria and SearchResults

SerachCriteria will hold the search criteria as supplied by end-user and your application logic will return an object of type SearchResults that is a collection of Cars.

You can maintain a flag within SearchResult corresponding to each car object in the result for if the owner is known.

this. __curious_geek
I liked both answers... I guess adding a SearchResults class is a pure way of maintaining the Car integrity. Might be overkill but it makes me sleep better at night.
RenderIn