25 July 2006

NHibernate Session Management

I'm big fan of NHibernate since early beta. Unfortunately this tool is not very popular among .NET developers, so I'm planning to blog a lot about this framework. For those of you who don't know, NHibernate is ORM (Object-Relational-Mapping) tool. Basically ORM tools are used to "persist" C# objects into RDBMS system. More detail explanation of ORM tools is available here.
NHibernate handles loading/saving objects to RDBMS via it's own implementation of session object. Until now, I'm familiar with 3 ways for handling NHibernate sessions:
  • create session per each database operation - session is created before and destroyed after each operation with database. This is very ineffective way, because some features like lazy loading are unavailable. Also including several database operations in transaction is impossible.
  • create session per each request - session is created before and destroyed after each HTTP request. This is my favorite strategy, because it is effective and very easy for implementation. I'll give example for possible implementation in this post.
  • create session per each application transaction - session is created before and destroyed after each application transaction. Example for application transaction is adding invoice with invoice items.

So, lets begin with the implementation of session per request strategy. If you are reading this post you are most likely a developer and probably as most developers prefer to read source code instead of boring documentation. So I'll explain all of the classes in brief and will post full source code. First I'll declare a base abstract class for all of the session strategies.



public abstract class OrmManager
{
    public OrmManager()
    {
    }
 
    static NHibernate.Cfg.Configuration mConfiguration = null;
    static volatile ISessionFactory mSessionFactory = null;
    static object mFactorySyncObject = new object();
 
    public abstract ISession session
    {
        get;
    }
 
    public IDbConnection sessionConnection
    {
        get { return session.Connection; }
    }
 
    public ISession NewSession()
    {
        ISessionFactory sessionFactory = GetSessionFactory();
 
        return sessionFactory.OpenSession();
    }
 
    public abstract bool isActiveSession
    {
        get;
    }
 
    public abstract void closeSession();
 
    private const string HBM_ASSEMBLY_NAME_SECTION = "HbmAssemblyName";
 
    private ISessionFactory GetSessionFactory()
    {
        if (mSessionFactory == null)
        {
            lock (mFactorySyncObject)
            {
                if (mSessionFactory == null)
                {
                    string hbmAssemblyName =
                        ConfigurationManager.AppSettings[HBM_ASSEMBLY_NAME_SECTION];
 
                    mConfiguration = new NHibernate.Cfg.Configuration();
                    mConfiguration.AddAssembly(hbmAssemblyName);
 
                    mSessionFactory = mConfiguration.BuildSessionFactory();
                }
            }
        }
 
        return mSessionFactory;
    }
 
    public void BeginTransaction(IsolationLevel isolation)
    {
        session.BeginTransaction(isolation);
    }
 
    public void CommitTransaction()
    {
        session.Transaction.Commit();
    }
 
    public void RollbackTransaction()
    {
        session.Transaction.Rollback();
    }
 
    public bool isActiveTransaction
    {
        get
        {
            return ((session.Transaction != null) &&
                (!session.Transaction.WasCommitted) &&
                (!session.Transaction.WasRolledBack));
        }
    }
}

Next, I'll implement the class responsible for session per request management. The following class implements IHttpModule so, we can handle end of the request and close any open sessions.


public class PerRequestWebManager : OrmManager, IHttpModule
{
    public PerRequestWebManager()
    {
    }
 
    private const string NHIBERNATE_SESSION_KEY = "NHIBERNATE_SESSION";
 
    public override ISession session
    {
        get
        {
            ISession resultSession;
 
            if (HttpContext.Current.Items.Contains(NHIBERNATE_SESSION_KEY))
            {
                resultSession =
                    (ISession)HttpContext.Current.Items[NHIBERNATE_SESSION_KEY];
            }
            else
            {
                resultSession = NewSession();
                HttpContext.Current.Items[NHIBERNATE_SESSION_KEY] = resultSession;
            }
 
            return resultSession;
        }
    }
 
    public override bool isActiveSession
    {
        get { return HttpContext.Current.Items.Contains(NHIBERNATE_SESSION_KEY); }
    }
 
    public override void closeSession()
    {
        if (isActiveSession == true)
        {
            if (isActiveTransaction == true)
            {
                RollbackTransaction();
            }
 
            session.Close();
            HttpContext.Current.Items.Remove(NHIBERNATE_SESSION_KEY);
        }
    }
 
    #region IHttpModule
 
    public void Init(HttpApplication context)
    {
        context.EndRequest += new EventHandler(OnEndRequest);
    }
 
    public void Dispose()
    {
        //Intentionaly not implemented
    }
 
    public void OnEndRequest(Object sender, EventArgs e)
    {
        closeSession();
    }
 
    #endregion
}

Now I'll make one generic DAO class with the help of previos PerRequestWebManager.


public class GenericDAO<T>
{
    public GenericDAO()
    {
        orm = OrmManagerFactory.GetInstance();
    }
 
    private OrmManager orm;
 
    public T Load(object id)
    {
        return (T) orm.session.Load(typeof(T), id);
    }
 
    public void Save(T entity)
    {
        bool manageTransaction = ! orm.isActiveTransaction;
        if (manageTransaction)
        {
            orm.BeginTransaction(IsolationLevel.ReadCommitted);
        }
        try
        {
            orm.session.SaveOrUpdate(entity);
 
            if (manageTransaction)
            {
                orm.CommitTransaction();
            }
        }catch(Exception e)
        {
            if (manageTransaction)
            {
                orm.RollbackTransaction();
            }
 
            throw e;
        }            
    }
}

public class OrmManagerFactory
{
    private OrmManagerFactory()
    {
    }
 
    private const string ORM_MANAGER_TYPE_SECTION = "OrmManager";
 
    public static OrmManager GetInstance()
    {
        string ormType = ConfigurationManager.AppSettings[ORM_MANAGER_TYPE_SECTION];
        if (ormType == typeof(PerRequestWebManager).Name)
        {
            return new PerRequestWebManager();
        }
        else
        {
            throw new Exception("Unknown ORM manager type");
        }
    }
}

And here's how all of the above can be used in ASP.NET application:


GenericDAO<Article> articleDao = new GenericDAO<Article>();
 
Article article = articleDao.Load(1);
article.Name = "Paracetamol";
article.SalePrice = rnd.NextDouble();
articleDao.Save(article);

If you are interested I can mail you full source of this post. Enjoy!

18 July 2006

Free Microsoft Office SharePoint Server 2007 Book

If you are interested in SharePoint Products and Technologies don't miss to read
7 Development Projects for Microsoft Office SharePoint Server 2007 and Windows SharePoint Services 3.0. The book is free for download and is based on Beta 1 version of upcoming new version of SharePoint products.

17 July 2006

Formatting source code posted in blog

Today I found a very useful tool for formatting a source code represented in HTML format. The tool is named CSAH (Copy Source As Html) and is actually a plug-in for Visual Studio 2003/2005. If you are interested, download it from here. Below is a screenshot from the plug-in in action.





BTW, the source code in my previos post about GridView is formatted with this tool.

14 July 2006

GridView’s header and empty DataSource

The successor of DataGrid in ASP.NET 2.0 – GridView behaves unusually sometimes. For example if binded ObjectDataSource is empty, then GridView’s header is not rendered. This is not the required behavior in most of the cases. If you want to show the header of the GridView when binded data source is empty, there are several solutions, but I will describe the one I like most.

Solution in brief: Subclass GridView control, override CreateChildControls method and manualy create header if binded DataSource is empty. The implementation of CreateChildControls method may look like:



protected override int CreateChildControls(IEnumerable dataSource,
    bool dataBinding)
{
    int numRows = base.CreateChildControls(dataSource, dataBinding);
 
    if (numRows == 0)
    {
        Table table = new Table();
        table.ID = this.ID;
 
        GridViewRow row = base.CreateRow(-1, -1,
                DataControlRowType.Header, DataControlRowState.Normal);
 
        DataControlField[] fields = new
                DataControlField[this.Columns.Count];
        this.Columns.CopyTo(fields, 0);
        this.InitializeRow(row, fields);
        table.Rows.Add(row);
        this.Controls.Add(table);
    }
 
    return numRows;
}


The code above adds only a header to the grid. If you need you can add another empty row, where appropriate message may be shown.

Adding footer is a little bit tricky. GridView exposes a public property for managing the footer. So the footer added by you, must be accessible via this property. The solution to the problem is to override the virtual property FooterRow (To Microsoft ASP.NET 2 Team: Thank you for making this property virtual. Unfortunately some methods in ASP.NET 2 are not virtual and extending the framework sometimes is not so easy). According to the explanations above the implementation may look like:



private GridViewRow mFooterRow = null;
 
public override GridViewRow FooterRow
{
    get
    {
        if (mFooterRow != null)
        {
            return mFooterRow;
        }
        else
        {
            return base.FooterRow;
        }
    }
}
 
protected override int CreateChildControls(IEnumerable dataSource,
    bool dataBinding)
{
    this.mFooterRow = null;
 
    int numRows = base.CreateChildControls(dataSource, dataBinding);
 
    if (numRows == 0)
    {
        Table table = new Table();
        table.ID = this.ID;
 
        GridViewRow row = base.CreateRow(-1, -1,
            DataControlRowType.Header, DataControlRowState.Normal);
 
        DataControlField[] fields = new
            DataControlField[this.Columns.Count];
        this.Columns.CopyTo(fields, 0);
        this.InitializeRow(row, fields);
        table.Rows.Add(row);
 
        this.mFooterRow = base.CreateRow(-1, -1,
            DataControlRowType.Footer, DataControlRowState.Normal);
        fields = new DataControlField[this.Columns.Count];
        this.Columns.CopyTo(fields, 0);
        this.InitializeRow(mFooterRow, fields);
        table.Rows.Add(mFooterRow);
        this.Controls.Add(table);
    }
 
    return numRows;
}

During the creation of the component I remembered the old Delphi days. Creating new .NET control is almost as easy and funny as it was in Delphi.

WSS2 SP2 and .NET 2 Bug

A few weeks ago I was surprised by a very annoying bug in Windows SharePoint Services SP2 running under .NET Framework 2.0. When a page is customized with FrontPage (in this case the layout of the page is stored in SQL Server) and web part compiled with .NET 2.0 is imported to this page, the following exception is thrown:

Invalid postback or callback argument. Event validation is enabled using
<pages enableEventValidation="true"/> in configuration or
<%@ Page EnableEventValidation="true" %> in a page. For security
purposes, this feature verifies that arguments to postback or callback events
originate from the server control that originally rendered them. If the data is
valid and expected, use the ClientScriptManager.RegisterForEventValidation
method in order to register the postback or callback data for validation.


After some quick and unsuccessful attempts to resolve the exception, rapid search in google proved that this is really a bug.

Fortunately this bug is not present in SharePoint 2007 Beta 2 (I have tested it).