I need to create a WCF service to return objects queried from a database through Entity Framework. Most articles I've read suggests that I should create a method for each type of query. But I think this will cause an explosion in the number of methods I will create, for example:
- GetAllCars()
- GetCarsByBrand(string brandName)
- GetCarsByYear(int year)
- GetCarsByBrandAndYear(string brandName, int year)
- GetCarsByTireSize(float tireSize)
- GetCarsByEngineType(string engineType)
- GetCarsByEngineSizeAndType(float engineSize, string engineType)
- GetCarsByEngineSizeBetween(float lowerEngineSize, float upperEngineSize)
- etc..
On top of that, if a new query is required, then I will have to create a new method to supoport it.
There must be a better more generic way to do this. What would be ideal is if the client can create an expression tree via LINQ, send it over WCF, then run the query through entity framework. Then I can have one method to support all queries. For example:
- QueryCars(Expression expression)
Or send an expression as a string:
- QueryCars(string expression)
How have developers solved this problem of flexible querying?
I am currently working in .NET 4.0. Security is not really a concern since this is just an internal app.