Click here to Skip to main content
15,896,344 members
Articles / Programming Languages / C#

Writing NHibernate Level 2 Cache Usage Tests

Rate me:
Please Sign up or sign in to vote.
4.69/5 (7 votes)
15 Jan 2009CPOL8 min read 53K   121   22  
Writing NHibernate Level 2 caching related tests by utilizing a custom log4net appender.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Burrow;
using Domain;

namespace Tests
{
  /// <summary>
  /// NHibernate helper class. Intended for SessionFactory and Session management
  /// helper functionality.
  /// </summary>
  public static class NHibernateHelper
  {

    public static void EvictSecondLevelCache()
    {
      BurrowFramework bf = new BurrowFramework();

      ISessionFactory sFactory = bf.GetSessionFactory(typeof(Parent));
      sFactory.EvictQueries();
      IDictionary classMetadata = sFactory.GetAllClassMetadata();
      foreach (Type t in classMetadata.Keys)
      {
        sFactory.Evict(t);
      }

      IDictionary collectionMetadata = sFactory.GetAllCollectionMetadata();
      foreach (string role in collectionMetadata.Keys)
      {
        sFactory.EvictCollection(role);
      }
    }

    public static void EvictSecondLevelQueryCache(string region)
    {
      BurrowFramework bf = new BurrowFramework();

      ISessionFactory sFactory = bf.GetSessionFactory(typeof(Parent));
      if (String.IsNullOrEmpty(region))
        sFactory.EvictQueries();
      else
        sFactory.EvictQueries(region);
    }

    public static void EvictSecondLevelQueryCache()
    {
      EvictSecondLevelQueryCache(String.Empty);
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader PragmaTouch
Turkey Turkey
- Software developer
- Has BS degree in Computer Engineering
- Has MBA degree
- Programmed with C, C++, Delphi, T-SQL and recently C#
- Little educational experience with Prolog
- Feel enthusiasm about NHibernate and LINQ
- Love to develop on Cuyahoga Web Framework
- Developer of PragmaSQL Editor
(Code Project Members Choice Winner for 2009 and 2010)
- Developed JiraTouch and MoodleTouch for iPhone
- PragmaTouch Lead (www.pragmatouch.com)

Comments and Discussions