tags:

views:

27

answers:

2

Is there a best practice on how to handle DAL methods with complex search logic? I'm talking about the business requirements like

"List customers who's companies are active or in sleep status, have more than 1000$ of orders in the last quarter, customer must not be deleted, company must have office in London or New York and Paris..."

These requirements quickly make your CustomerDAL.GetCustomers(...) have over 9000 parameters, not to mention that it changes all the time which can be a pain if you have lots of layers, interfaces, webservices etc.

Are there other good ways to clean up methods like that, besides creating a CustomerSearchParameters struct which you create and set up before calling the method itself?

+1  A: 

Well you could build an expression within the method calling GetCustomers() and then pass in that expression into the query, e.g.

GetCustomer(string where)

However this makes validating the where clause difficult. A better approach might be to use a expression structure to capture the constraints, this would also make it easier to validate the expression prior to executing the query:

class Expression {
  string PropertyName
  object value
}

GetCustome(Expression[] constraints)
Obalix
+1  A: 

Maybe you should have a look at the JPA Criteria API. Even if you are not using JPA a model like this where you can add Criteria to your query dynamically might do what you need. I would recommend to make sure, that you create a transparent API (your DAL or DAO methods e.g. shouldn't rely on the JPA Criteria API directly).

Daff