Archive for September, 2007
Searching with NHibernate
In these days, while working with NHibernate, I was building a method that load some data using an ICriteria and the Visual Studio 2008 Intellisense had show me a class named Example.
After a bit research throught the source code I understand it’s usage: this class enables your application to use a “partial-populated object” to build the where clause. Let’s see an example
Suppose that you need to load all Orders in state accepted at date 09/04/2007. You can write this code:
Order order = new Order();
order.Stato = OrderState.Accepted;
order.Date = new DateTime(2007, 04, 09);
ICriteria criteria = session.CreateCriteria(typeof(Order));
criteria.Add(Example.Create(order));
IList result = criteria.List();
What Nhibernate do is to parse all order’s property that have a non-default value and use that values to build a filter.
These method seems very useful to implement search functionality in your applcation.
RSS


