NHibernate - register items displayed in search results

I am using NHibernate in an MVC 2.0 application. Basically, I want to track the number of times each product appears in search results. For example, when someone searches for a widget, a product named WidgetA will appear on the first page of search results. At this point, I'll grow the database field to reflect that it appeared as part of the search result.

While this is straight forward, I'm concerned that the inserts themselves slow down the search result significantly. I would like to merge my statements together, but it seems that pairing my inserts with my choice might be counterproductive. Has anyone tried to do this in NHibernate, and if so are there any standard patterns for completing this kind of operation?

+2


a source to share


1 answer


Interest Ask!

Here's a possible solution:

var searchResults = session.CreateCriteria<Product>()
    //your query parameters here
    .List<Product>();
session.CreateQuery(@"update Product set SearchCount = SearchCount + 1
                      where Id in (:productIds)")
       .SetParameterList("productIds", searchResults.Select(p => p.Id).ToList())
       .ExecuteUpdate();

      



You can of course search using criteria, HQL, SQL, Linq, etc.

The update request is a one-time trip for all objects, so the performance impact should be minimal.

+1


a source







All Articles