views:

109

answers:

1

I'm looking to build complex queries using the NHibernate Criteria API. I'd like to verify that the criteria is constructed as I would expect without having to actually run the query.

Is this possible? Are there any tips or techniques for doing it elegantly?

+2  A: 

A co-worker recently did this

encapsulate each criteria query in it's own class (specification). built an expression builder that will spit out the query to the string. tested that the generated expression string matches the expected expression string.

the expression builder walked the criteria tree in a specification in a recursive fashion to generate the expression string.

that's the general idea, don't have the code with me right now :) sorry.

However, I've found that it's much easier to write db integration tests to test Nh criteria. Does the criteria really return the data I expect.

Hibri
Sounds similar to what I'm going to do. Is this using the visitor pattern? One that builds a string of the names of the specifications and one that builds a criteria using the specifications? You test the former but use the latter?
Garry Shutler
Yes, that's what it is. Haven't found any other approach yet.
Hibri