2

I have a controller that uses Spring to automatically map HTTP request params to business domain objects. I persist the field data in the domain objects to a database.

I have another controller that needs to support searching on any combination of fields related to the domain objects. For some of the fields (e.g. date fields), I need to allow searching within a range. So, I can't reuse the domain objects since it only has a single date field.

For example, I have a Report domain object with a date field. However, the search needs a "from" Report date field and a "to" Report date field.

Should I create a set of domain objects for just searching that mostly mirror the business domain objects (except for fields that support range searches)? Or is there some better way to do this?

Thanks in advance for your help.

James
  • 2,876
  • 18
  • 72
  • 116

2 Answers2

0

How about adding two fields in existing domain object for range instead of creating new object?
Like adding two new fields fromReportDate and toReportDate to your Report domain object.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
0

Could you consider introducing a Criteria-like object instead?

I wouldn't create more business objects based on what you currently have, simply because you're not modelling those entities - you're modelling the search criteria used to find them.

For example, you might do the following:

CriteriaBuilder<Report> builder = new CriteriaBuilder<Report>();
builder.addFrom("date", new Date())
builder.addTo("date", new Date());
Criteria<Report> criteria = builder.build();

The CriteriaBuilder could reflectively check the class properties to make sure you're not attempting to bind a criterion against an absent field.

David Grant
  • 13,929
  • 3
  • 57
  • 63