Handling Dynamic Queries with the Entity Framework
Insomniac dreams. I am awake since about 2:30 AM (it is 07:30AM now), which meant that I had a chance to read my feeds, catch on my emails, do some OSS work, release a new beta and post too much :-). Just thought that I should explain why you see so many posts all of a sudden.
Anyway, the reason for this post is asking how the ADO.Net Entity Framework will handle dynamic queries. I am talking about something like this example. I just read John's post about the Entity Framework, and the question just came up in my mind.
Comments
刚刚接触Ado.net vNext一段时间后,我就觉得他在动态查询方面将会有巨大用途...... 在我们现有的很多项目中,设计到很多联合查询,用户自定义查询等等动态类型的查询.例如,在一个人事系统中,想要查询某一位员工,我可能提供诸如姓名,性别,籍贯,所在组织,所属领导,工作类别,薪水...等等的查询条件,用户可以任意选择,组合查询条件.而我们通常的做法都是通过判断用户选择的条件,拼凑一段SQL语句,然后交由数据库执行(我不知道在一个纯ORM方案中怎么解决).这样做,繁琐,易出错,难调试...缺点不用再多列.
I agree with 吴頔 ;)
Kidding aside, I think dyn. queries is indeed an overlooked point in Linq in general. It will likely come down to building expression trees by yourself, something you don't want to do, especially since you can build Linq support with solely extension methods so you don't need to manipulate expression trees.
Another overlooked point in Linq is that with the more complex stuff it's hard to determine where a given expression / function is evaluated: in the DB or in memory. This could hurt performance in the long run: if the developer thinks a function is ran in the db while it's evaluated in memory, it could lead to very slow queries with big data. Sadly enough, developers often don't catch that kind of mistakes until it's too late when the app is long in production.
If it is true that the only ways I have to build dynamic queries is by dealing with the AST directly, this is a major point against Linq in general.
I routinely build complex queries according to business logic.
Not just search pages, but "give me the data that matches the following criteria" - where the criteria is different based on logic.
AST is cool for tech stuff, not so cool when you are trying to use it for business logic.
The "is it in memory" vs "is it in DB" is going to be a killer, I agree.
Hi there, I'm currently using your query builder tool for nhibernate, actually I'm using it with ActiveRecord, and I was wondering how that handles dynamic queries. Can it do the same as the linked post example?
I can keep adding criteria to a QueryBuilder<Account> as long as it is Query.Where.Account... but if I have a list of Contact objects under Account i.e. there is a linked Contacts table I cannot add Query.Where.Contact... I think!
Otherwise I am becoming a big fan of your blog, and try to introduce concepts I find here and posted in the comments into my everyday development. Hence my question :-)
Yes, you can use the linked post for reference, it also shows how to handle collections.
With the EF you can build dynamic queries by creating the expression trees manually, or by using eSQL. I guess you could build a framework on top of either one to make it easier.
Comment preview