Filter through model or dao?

How should I filter a series of domain objects according to user-defined criteria? Should filtering methods be in the model or should be in the DAO?

+3
source share
2 answers

If you want to use only model objects (mostly) as data containers, you should put a filter in the DAOs that you already use. It’s good practice to make sure that user criteria are independent of the database (so pass your own filter object, not a simple one like Hibernate Criteria. An example query may work fine too).

Then your DAO methods might look like this:

public interface BeanDao
{
    List<Bean> findAllByFilter(BeanFilter filter);
}
+3
source

The choice of whether to retrieve more objects and then filter or just retrieve the correct objects in the first place depends on the underlying data. Most applications will use a combination of the two.

I would consider the following things:

Network bandwidth and memory requirements

If after filtering there are a small number of results, but a significantly larger number of results before filtering, then this can be a waste of resources and memory resources for filtering in code.

Request speed

Filtering results in a database can be more expensive than executing logic in code β€” disk and memory. Indexes are needed to make this useful.

maintainability

. sql . db, , .

Java, , . SingleMatcher MultipleMatcher . . , , , , .

+1

Source: https://habr.com/ru/post/1756475/


All Articles