NHibernate Tidbits: Serializing ICriteria
Quite a few parts of NHibernate are serializable. ISession and ICriteria are of particular interest to this post. Serializing the session (neccesiate that the entities are serializable, of course) is very useful for the Session-pre-conversation mode. But it is the criteria that I would like to talk about right now.
Criteria in NHibernate is the OO interface for making queries. So, why is it important that it is serializable? Well, it opens up some interesting scenarios. Saved searches or reports are just one of many. I have used similar functionality in the past to build a rule engine, where saved criteria where the basis of selecting what a rule should process.
The only problem with that is that you usually want to display the query to the user. I have recently added inspection capabilities to the criteria, but they are focused more on infrastructure stuff rather than ease of extraction for the UI. For those cases, you might want to add a way or the UI to restore itself to the appropriate state for the selected search.
Comments
I've actually used this one in the past.
Example:
You have a SalesReport that includes different types of SalesItems. How do you find the different SalesItems relevant to a SalesReport?
The simplest way, in my anecdote, was to serialize the SalesItem criteria as a property of the SalesReport.
No complaints from the client on that one, so everyone seems to be happy with this one :)
Comment preview