Pulagara Murali

Pulagara Murali

  • NA
  • 43
  • 43.6k

SessionFactory

Oct 13 2012 1:02 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;

namespace Symphony.Phoenix.Hibernate
{
    public class NHibernateFactory
    {
        #region Private Variables
   
    private static ISessionFactory sessionFactory;
    #endregion

     #region Static Constructor

    static NHibernateFactory()
    {
      log4net.Config.XmlConfigurator.Configure();
    }

    #endregion

     #region Static Methods

public static object GetCurrentSession()
    {
      object Hsession= sessionFactory.OpenSession();

 //IPSession session = Hsession ;
      //session = currentSession;
      
     // IPSession _CurrentSession=currentSession as IPSession;

 return Hsession;
    }

    public static void CloseSessionFactory()
    {
      if (sessionFactory != null)
      {
        sessionFactory.Close();
      }
    }

    public static void SelectDB(string Database)
    {
string mappingPath = AppDomain.CurrentDomain.BaseDirectory + @"bin\MappingXML\RuleEngineExecutor\";      
      Configuration config = new Configuration();

      //Select the Database for NHibernate
      if (Database == "SQLSERVER")
      {
        config.SetProperty("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
        config.SetProperty("dialect", "NHibernate.Dialect.MsSql2005Dialect");
        config.SetProperty("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
        config.SetProperty("connection.connection_string", "Server=192.168.1.23;Initial Catalog=RuleEngine;User Id=tenxlabs;Password=123456");    
        config.SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");
   
      }

      if (Database == "ORACLE")
      {
        config.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
        config.SetProperty("hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect");
        config.SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
        config.SetProperty("hibernate.connection.connection_string", "Server=tenx01-PC;Initial Catalog=Employees;User Id=sa;Password=test");
      }

      if (Database == "MYSQL")
      {
        config.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
        config.SetProperty("hibernate.dialect", "NHibernate.Dialect.MySQL5Dialect");
        config.SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.MySqlDataDriver");
        config.SetProperty("hibernate.connection.connection_string", "Server=TENX04-PC\\SQLEXPRESS;Initial Catalog=Employees;Integrated Security=True");
      }

      config.AddFile(mappingPath + @"RuleEngineFunctionRules.hbm.xml");
     
      sessionFactory = config.BuildSessionFactory();
    }
    #endregion
    }
}