Monday, March 8, 2010

NHibernate - Ad Hoc SQL GetDate

Lately I have been focusing on designing a web project to serve as a platform (call it white label) and I will blog more about my experiences on that. Here, I'm going to give a tip using NHibernate. We were building a health check page for our monitors and all it has to do is make a simple database call. What is more appropriate than getting current date.

Doing this in NHibernate is a quite simple (for that matter, any ad-hoc SQL). All you need to know is about CreateSQLQuery for the session and returning the UniqueResult.

ISQLQuery sqlQuery = session.CreateSQLQuery("select getdate()");
return sqlQuery.UniqueResult<DateTime>();

If you are interested in a plug and play code example, here it is...
First, a simple NHibernate utility class:

using NHibernate;
using NHibernate.Cfg;

namespace MyNamespace
{
    /// <summary>
    /// Author: Kishore Gorjala
    /// </summary>
    internal sealed class NHibernateUtility
    {
        private static ISessionFactory _factory;

        #region "Public Properties"
        /// <summary>
        /// SessionFactory - Very expensive to create and hence should be singleton
        /// </summary>
        internal static ISessionFactory Factory
        {
            get
            {
                if (_factory == null)
                {
                    Configuration cfg = new Configuration();
                    cfg.Configure();
                    _factory = cfg.BuildSessionFactory();
                }
                return _factory;
            }
        }
        
        /// <summary>
        /// Session 
        /// </summary>
        internal static ISession Session
        {
            get
            {
                return Factory.OpenSession();
            }
        }
        #endregion "Public Properties"
    }
}
And here is the controller class:

using NHibernate;
using System;

namespace MyNamespace.DomainControllers
{
    /// <summary>
    /// HealthCheckController
    /// Author: Kishore Gorjala
    /// </summary>
    public class HealthCheckController
    {
        public static DateTime DatabaseTime
        {
            get
            {
                using (ISession session = DataLayer.NHibernateUtility.Session)
                {
                    ISQLQuery sqlQuery = session.CreateSQLQuery("select getdate()");
                    return sqlQuery.UniqueResult<DateTime>();
                }
            }
        }
    }
}
Love coding!

Tuesday, January 12, 2010

Virtual applications under Ektron hosted sites - virtual folders

This is a quick tip for those who are wondering on why they get errors with httpHandlers and httpModules when they create virtual applications under a site hosting Ektron based root.

You just need to remove the references to get rid of Ektron handlers. A simple config settings (Web.config) would look like (for your virtual hosted application):


<httpHandlers>

<remove verb="HEAD,OPTIONS,PROPFIND,PUT,LOCK,UNLOCK,MOVE,COPY,GETLIB,PROPPATCH,MKCOL,DELETE,(GETSOURCE),(HEADSOURCE),(POSTSOURCE)" path="*" />
<remove verb="GET,POST" path="*.doc" />
<remove verb="GET,POST" path="*.docx" />
<remove verb="GET,POST" path="*.xls" />
<remove verb="GET,POST" path="*.xlsx" />
<remove verb="GET,POST" path="*.ppt" />
<remove verb="GET,POST" path="*.pptx" />
<remove verb="GET,POST" path="*.vsd" />
<remove verb="GET,POST" path="*.vsdx" />
<remove verb="*" path="*/wfactivities.png" />

</httpHandlers>

<httpModules>

<remove name="DigestAuthenticationModule" />
<remove name="EkUrlAliasModule" />

</httpModules>

Love coding!