Finding Content

There are multiple ways of finding content stored in the database:

  • The finder fluent API (N2.Find.Items…)
  • Traversing the content tree (N2.Find.StartPage.GetChildren()…)
  • LINQ (using N2.Linq; Engine.QueryItems())
  • Using NHibernate query options (hql, criteria, raw SQL, LINQ)
     public ActionResult QueryForProperty()
     {
           ViewData["foundItmes"] = FindRecentlyPublishedWithFinder();
           ViewData["traversedItmes"] = FindRecentlyPublishedWithTraversion();
           ViewData["linqedItems"] = FindRecentlyPublishedWithLINQ();
           ViewData["hqledItems"] = FindRecentlyPublishedItemsWithNHHql();
           ViewData["criteriaItems"] = FindRecentlyPublishedItemsWithNHCriteria();
           ViewData["nhlinqedItems"] = FindRecentlyPublishedItemsWithNHLINQ();
 
           return View();
     }

The MVC action above invokes multiple ways of finding content which will be analyzed in further detail below.

     private IEnumerable<ContentItem> FindRecentlyPublishedWithFinder()
     {
           // this will create a hql expression behind the scenes and run it
           var recentlyPublishedItems = N2.Find.Items
                .Where.Published.Gt(DateTime.Now.AddMonths(-1))
                .Filters(new AccessFilter())
                .Select();
           return recentlyPublishedItems;
     }

This is example usage of the finder fluent API.

     private IEnumerable<ContentItem> FindRecentlyPublishedWithTraversion()
     {
           // this will force all contnet items in the database into memory
           var recentlyPublishedItems = Find.EnumerateTree(Find.RootItem)
                .Where(ci => ci.Published > DateTime.Now.AddMonths(-1))
                .Where(new AccessFilter().Match)
                .ToList();
           return recentlyPublishedItems;
     }

 

This is example usage of traversing the complete content hierarchy using a helper method.  This method will force all content to be loaded into server memory and isn’t optimal when there is a lot of content.

     private IEnumerable<ContentItem> FindRecentlyPublishedWithLINQ()
     {
           // the linq support is not fully functional in N2 2.1 beta
           var recentlyPublishedItems = Engine.QueryItems()
                .Where(ci => ci.Published > DateTime.Now.AddMonths(-1))
                .Where(new AccessFilter().Match)
                .ToList();
           return recentlyPublishedItems;
     }

This example uses the N2 LINQ  wrapper around NH LINQ  for finding items.

     private IEnumerable<ContentItem> FindRecentlyPublishedItemsWithNHHql()
     {
           // use NHibernate hql api for querying
           // will include previous versions by default
           var recentlyPublishedItems = Engine.Resolve<ISessionProvider>().OpenSession.Session
                .CreateQuery("from ContentItem ci where ci.Published > :minDate")
                .SetParameter("minDate", DateTime.Now.AddMonths(-1))
                .Enumerable<ContentItem>()
                .Where(new AccessFilter().Match)
                .ToList();
           return recentlyPublishedItems;
     }

 

This example falls back to the NHibernate hql API for querying. The results are not filtered for previous versions In this case. This is used behind the hood when using the N2 finder API.

     private IEnumerable<ContentItem> FindRecentlyPublishedItemsWithNHCriteria()
     {
           // use NHibernate criteria api for querying
           // will include previous versions by default
           var recentlyPublishedItems = Engine.Resolve<ISessionProvider>().OpenSession.Session
                .CreateCriteria<ContentItem>("ci")
                .Add(NHibernate.Criterion.Expression.Gt("Published", DateTime.Now.AddMonths(-1)))
                .List<ContentItem>()
                .Where(new AccessFilter().Match)
                .ToList();
           return recentlyPublishedItems;
     }

This example uses the NHibernate Criteria API for querying. The results are not filtered for previous versions.

     private IEnumerable<ContentItem> FindRecentlyPublishedItemsWithNHLINQ()
     {
           // use NHibernate linq api for querying
           // will include previous versions by default
           var recentlyPublishedItems = Engine.Resolve<ISessionProvider>().OpenSession.Session
                .Query<ContentItem>()
                .Where(ci => ci.Published > DateTime.Now.AddMonths(-1))
                .Where(new AccessFilter().Match)
                .ToList();
           return recentlyPublishedItems;
     }

This example uses the NHibernate LINQ API for querying. The results are not filtered for previous versions. This is used behind the hood when using the N2 LINQ API.

Finding details

Details are stored in a separate detail table. Querying for details means asking the database to do a sub-select.

     private object FindTextContainsWithFinder()
     {
           // this will cause a sub-select
           var itemsWithSomestring = N2.Find.Items
                .Where.Detail("Text").Like("%somestring%")
                .Filters(new AccessFilter())
                .Select();
           return itemsWithSomestring;
     }