Parameterizing complex search methods
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?
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)
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).
精彩评论