Asham
July 13th, 2008, 03:11 AM
Hello, it's me again. :-)
Property/filtered searches - how do *you* do them?
I am a Java programmer but can follow code in C++, C#, python
Hypothetical example:
we own a car shop where you can search for cars by
- brand name
- model name
- year
- engine
- gearbox
- tires
- rims
- color
- [et cetera]
So now the question is howto structure the DAO/DAO helper class in a well structured manner.
At first one could come up with a quick solution such as
List<Car> getCars(String brand, String model, String rims, String tires, String engine [and so on])
but this does not look efficient when we want to search for let's say just two attributes. Then we'd have to pass null arguments for properties we aren't going to search for, or to create a method for when searching for one attribute, another method for two attributes and so on. What if we were to remove an attribute from the search function?? :(
No, this is no viable way to go!
Another possibility could be to pass a Map of filters, such as
Map<String, String> filters = new LinkedHashMap<String, String>();
filters.put("brand", "Ford");
filters.put("model", "Falcon Futura");
List<Car> items = carDAO.getCars(filters);
Is this better? Yes, but not perfect either because we would have to memorize what filters that are searchable. That we can fix easily by adding an enum of search constants. We are also tied to using string objects.
Another way could be to implement the command pattern, and perhaps append filters to the command before it's executed.
What are your thoughts?
I am a Java programmer but can follow code in C++, C#, python
/Adam
Property/filtered searches - how do *you* do them?
I am a Java programmer but can follow code in C++, C#, python
Hypothetical example:
we own a car shop where you can search for cars by
- brand name
- model name
- year
- engine
- gearbox
- tires
- rims
- color
- [et cetera]
So now the question is howto structure the DAO/DAO helper class in a well structured manner.
At first one could come up with a quick solution such as
List<Car> getCars(String brand, String model, String rims, String tires, String engine [and so on])
but this does not look efficient when we want to search for let's say just two attributes. Then we'd have to pass null arguments for properties we aren't going to search for, or to create a method for when searching for one attribute, another method for two attributes and so on. What if we were to remove an attribute from the search function?? :(
No, this is no viable way to go!
Another possibility could be to pass a Map of filters, such as
Map<String, String> filters = new LinkedHashMap<String, String>();
filters.put("brand", "Ford");
filters.put("model", "Falcon Futura");
List<Car> items = carDAO.getCars(filters);
Is this better? Yes, but not perfect either because we would have to memorize what filters that are searchable. That we can fix easily by adding an enum of search constants. We are also tied to using string objects.
Another way could be to implement the command pattern, and perhaps append filters to the command before it's executed.
What are your thoughts?
I am a Java programmer but can follow code in C++, C#, python
/Adam