Saturday, April 6, 2013

Spool up Fluent NHibernate filtering conditions.

The thing I've been building here, here, and here now has the following Fluent NHibernate-flavored repository. Check out the second method below:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Criterion;
using ShockCaperShivers.Core;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
namespace ShockCaperShivers.Infrastructure
{
   public class HandRepository : IHandRepository
   {
      public void AddHand(Hand hand)
      {
         using (var sessionFactory = CreateSessionFactory())
         {
            using (var session = sessionFactory.OpenSession())
            {
               using (var transaction = session.BeginTransaction())
               {
                  session.Save(hand);
                  session.Flush();
                  transaction.Commit();
               }
            }
         }
      }
      
      public Hand[] RetrieveFirstFewUnmatchedHands()
      {
         using (var sessionFactory = CreateSessionFactory())
         {
            using (var session = sessionFactory.OpenSession())
            {
               session.CreateCriteria(typeof (Hand)).List();
               IList criteria = session
                  .CreateCriteria(typeof(Hand))
                  .Add(Restrictions.IsNull(MagicStringFactory.HandOpposing))
                  .AddOrder(Order.Asc(MagicStringFactory.TimestampIdentity))
                  .SetMaxResults(3)
                  .List();
               Hand[] handsSurvivingFiltering = GetHandsSurvivingFiltering<Hand>
                     (criteria).ToArray();
               session.Flush();
               return handsSurvivingFiltering;
            }
         }
      }
      
      public void UpdateHands(Hand handAtHand, Hand handOpposing)
      {
         using (var sessionFactory = CreateSessionFactory())
         {
            using (var session = sessionFactory.OpenSession())
            {
               using (var transaction = session.BeginTransaction())
               {
                  session.SaveOrUpdate(handAtHand);
                  session.SaveOrUpdate(handOpposing);
                  session.Flush();
                  transaction.Commit();
               }
            }
         }
      }
      
      private static IList<T> GetHandsSurvivingFiltering<T>(IList criteria)
      {
         IList<T> handsSurvivingFiltering = new List<T>();
         foreach (T value in criteria)
         {
            handsSurvivingFiltering.Add(value);
         }
         return handsSurvivingFiltering;
      }
      
      private static ISessionFactory CreateSessionFactory()
      {
         return Fluently.Configure().Database(MsSqlConfiguration.MsSql2005
               .ConnectionString(c => c.FromAppSetting("FluentNHibernateConnection")))
               .Mappings(m => m.FluentMappings.AddFromAssemblyOf<HandRepository>
               ()).BuildSessionFactory();
      }
   }
}

No comments:

Post a Comment