Object-oriented – DDD and filtering

Architecturedesign-patternsdomain-driven-designobject-oriented

I am developing an app in ddd maner. So I have a complex domain model. Suppose I have a Fare object and Airline. Each Airline should contain several or much more Fares.

My UI should represent Model (only small part of complex model) as a list of Airline, when the user select the Airline, I must show the list of Fares. User can filtering the Fares (by travel time, cost, etc.).

What is the appropriate place for filtering Fares and Airlines?

I am assuming that I should do it in ViewModel. Like: My domain model has wrapped with Service Layer -> UI works with ViewModel -> ViewModel obtain data from Service Layer filtering it and create DTO objects for UI. Or I'm wrong?

Best Answer

If there is a substantial amount of data to filter, then consider passing the "filter" query up through the stack so that it is a database query. This will minimize the amount of data passing between your database (assuming you have one) and your code.

On the other hand, if you need to filter data that are already in memory, and your code is all in a single process, then you can do so at any point: you are only passing pointers after all. But if you are considering refactoring your service layer into an actual service, running separately from the UI code, then you want the filtering to be in the service layer so that you can again minimize the data traffic between layer boundaries.