views:

20

answers:

2

I'm writting a general DAO interface and facing a difficult in general Query operation. Query operation requires a set of query conditions. I'd like to give a interface like

IList<TEntity> Query(DetachedCriteria criteria);

But this makes the interface rely on nHibernate. If I want to use Linq or plain SQL, it is not easy to convert DetachedCriteria to them.

I want to have a class that can wrap the query conditions just like DetachedCriteria in nHibernate, but can be converted to DetachedCriteria, or linq, or plain SQL.

I've once written a class in Java which wraps the query conditions and can be converted into plain SQL. I want to known whether there is any existing project in dot net that can do similiar work.

A: 

You can use IQueryable and build expression tree (the way which .NET Framework uses to build general query) or you can implement your own Specification pattern.

Ladislav Mrnka
Thank you for your suggestion!
rudaoshi
A: 

How about

Expression<Predicate<TEntity>>

You can then parse the expression tree and transform it into SQL. For linq you can even simply transform it into a where clause with the same predicate.

http://blogs.msdn.com/b/mattwar/archive/2008/11/18/linq-links.aspx describes the building of a complete Linq Provider, but you probably only need the equivalent of a where clause.

CodeInChaos
Thank you very much for providing me a good meterial. I will wirte a ICriteria provider.
rudaoshi