NhHibernate

我的想法是做一个统一的ISession提供者,只需要简单的配置即可用在不同的应用程序里。

储存ISession类需要实现的接口。
/**/ /*
 * 修改日期:2005-10-05
 * 修改人:DDL
 * 修改原因:
 * 
*/

using  NHibernate;

namespace  Index.Data.NHibernateSessionStorage
{
    
/**//// 


    
///储存一个ISession
    
/// 

    public interface ISessionStorage
    
{
        
/**//// 
        
///获得ISession 
        
/// 

        
/// 

        ISession Get();

        
/**//// 
        
/// 保存ISession
        
/// 

        
/// 

        void Set(ISession value);
    }

}

非Asp.Net程序使用的ISession提供者
/**/ /*
 * 修改日期:2005-10-10
 * 修改人:DDL
 * 修改原因:
 * 
*/


using  System;
using  NHibernate;

namespace  Index.Data.NHibernateSessionStorage
{
    
/**//// 
    
/// 保存一个Session在一个thread-static的类成员中。
    
/// 

    public class ThreadSessionSource : ISessionStorage
    
{
        [ThreadStatic] 
        
private static ISession m_Session;

        
/**//// 
        
///获得Session 
        
/// 

        
/// 

        public ISession Get()
        
{
            
if (m_Session != null)
            
{
                
if (!m_Session.IsConnected)
                
{
                    m_Session.Reconnect();
                }

            }

            
return m_Session;
        }


        
/**//// 
        
/// 保存Session
        
/// 

        
/// 

        public void Set(ISession value)
        
{
            
if (value.IsConnected)
            
{
                value.Disconnect();
            }

            m_Session 
= value;
        }

    }

}


Asp.Net程序使用的ISession提供者
/**/ /*
 * 修改日期:2005-10-05
 * 修改人:DDL
 * 修改原因:
 * 
*/


using  NHibernate;
using  System.Web;
using  Index.Data.NHibernateSessionStorage.CFG;

namespace  Index.Data.NHibernateSessionStorage
{
    
/**//// 
    
/// 储存一个ISession  集合.
    
/// 

    public class HttpSessionSource : ISessionStorage 
    
{
        
/**//// 
        
/// 获得ISession 
        
/// 

        
/// 获得的ISession

        public ISession Get() 
        
{
            
return (ISession)HttpContext.Current.Items[Config.HttpSessionSourceItemName];
        }


        
/**//// 
        
/// 保存ISession
        
/// 

        
/// 需要保存的ISession

        public void Set(ISession value) 
        
{
            
if (value != null)
            
{
                HttpContext.Current.Items.Add(Config.HttpSessionSourceItemName, value);
            }

            
else
            
{
                HttpContext.Current.Items.Remove(Config.HttpSessionSourceItemName);
            }

        }

    }

}


通过读取配置文件让工厂提供不同的ISession提供者
/**/ /*
 * 修改日期:2005-10-05
 * 修改人:DDL
 * 修改原因:
 * 
*/


using  System;
using  Index.Data.NHibernateSessionStorage.CFG;

namespace  Index.Data.NHibernateSessionStorage
{
    
/**//// 
    
/// 产生ISessionStorage的工厂
    
/// 

    public class ISessionStorageFactory
    
{
        
/**//// 
        
/// 获得ISessionStorage
        
/// 

        
/// 

        public static ISessionStorage GetSessionStorage()
        
{
            
if(Config.SessionSourceType=="http")  //使用    
            {
                
return new HttpSessionSource();
            }

            
else if(Config.SessionSourceType=="threadStatic")      
            
{
                
return new ThreadSessionSource();
            }

            
else
            
{
                
throw new NotSupportedException("不支持的SessionSourceType!" + Config.SessionSourceType);
            }

        }

    }

}


配置类
/**/ /*
 * 修改日期:2005-10-05
 * 修改人:DDL
 * 修改原因:
 * 
*/



using  System;
using  System.Configuration;

namespace  Index.Data.NHibernateSessionStorage.CFG
{
    
/**//// 
    
/// 配置信息帮助类
    
/// 

    public class Config
    
{
        
私有成员#region 私有成员

        
private static object m_Locker = new object();
        
private static string m_SessionSourceType=String.Empty;
        
private static string m_HttpSessionSourceItemName=String.Empty;

        
#endregion


        
属性#region 属性

        
/**//// 
        
/// Session资源源类型;http,threadStatic
        
/// 

        public static string SessionSourceType
        
{
            
get
            
{
                
lock( m_Locker )
                
{
                    
if(m_SessionSourceType==String.Empty)
                    
{
                        
return ConfigurationManager.AppSettings["SessionSourceType"];
                    }

                    
else
                    
{
                        
return m_SessionSourceType;
                    }

                }

            }

        }


        
/**//// 
        
/// HttpSessionSource存放HttpContext.Current.Items的键值名
        
/// 

        public static string HttpSessionSourceItemName
        
{
            
get
            
{
                
lock( m_Locker )
                
{
                    
if(m_HttpSessionSourceItemName==String.Empty)
                    
{
                        
return ConfigurationManager.AppSettings["HttpSessionSourceItemName"];
                    }

                    
else
                    
{
                        
return m_HttpSessionSourceItemName;
                    }

                }

            }

            
        }


        
/**//// 
        
/// 是否使用Session资源源
        
/// 

        public static bool UserSessionSource
        
{
            
get
            
{
                
lock (m_Locker)
                
{
                    
return Convert.ToBoolean(ConfigurationManager.AppSettings["UserSessionSource"]);
                }

            }

        }


        
#endregion

    }

}



然后进行其他的一些封装操作
/**/ /*
 * 修改日期:2005-10-05
 * 修改人:DDL
 * 修改原因:生成工厂类
 * 
*/


using  Index.Data.NHibernateSessionStorage.CFG;
using  NHibernate;
using  NHibernate.Cfg;

namespace  Index.Data.NHibernateSessionStorage
{
    
/**//// 
    
/// 用来生成ISession实例的工厂
    
/// 

    public static class NHibernateDatabaseFactory
    
{
        
私有静态变量#region 私有静态变量

        
private static object m_Locker = new object();
        
private static Configuration m_Configuration = null;
        
private static ISessionFactory m_SessionFactory = null;
        
private static ISessionStorage m_Sessionsource;

        
#endregion


        
静态构造函数#region 静态构造函数

        
static NHibernateDatabaseFactory()
        
{
            m_Sessionsource 
= ISessionStorageFactory.GetSessionStorage();
        }


        
#endregion


        
内部静态变量#region 内部静态变量

        
/**//// 
        
/// NHibernate配置对象
        
/// 

        public static Configuration Configuration
        
{
            
get
            
{
                
lock (m_Locker)
                
{
                    
if (m_Configuration == null)
                    
{
                        CreateConfiguration();
                    }

                    
return m_Configuration;
                }

            }

            
set { m_Configuration = value; }
        }


        
/**//// 
        
/// NHibernate的对象工厂
        
/// 

        internal static ISessionFactory SessionFactory
        
{
            
get
            
{
                
if (null == m_SessionFactory)
                
{
                    
if (m_Configuration == null)
                    
{
                        CreateConfiguration();
                    }

                    
lock (m_Locker)
                    
{
                        m_SessionFactory 
= Configuration.BuildSessionFactory();
                    }

                }


                
return m_SessionFactory;
            }

        }


        
#endregion


        
公共方法#region 公共方法

        
/**//// 
        
/// 建立ISessionFactory的实例
        
/// 

        
/// 

        public static ISession CreateSession()
        
{
            
if (Config.UserSessionSource) //如果使用保存的ISession
            {
                ISession s 
= m_Sessionsource.Get();
                
if (s == null)
                
{
                    s 
= SessionFactory.OpenSession();

                    m_Sessionsource.Set(s);
                }

                
return s;
            }

            
else //如果使用新ISession
            {
                
return SessionFactory.OpenSession();
            }

        }


        
#endregion


        
私有方法#region 私有方法

        
private static void CreateConfiguration()
        
{
            m_Configuration 
= new Configuration();
            
// Add interceptor, if you need to.
            
// _config.Interceptor = new Interceptor();
        }


        
#endregion

    }

}

使用方法
非Asp.net应用配置
   < appSettings >
    
< add  key ="SessionSourceType"  value ="threadStatic"   />
    
< add  key ="HttpSessionSourceItemName"  value ="NHSession"   />
    
< add  key ="UserSessionSource"  value ="true" />
  
appSettings >
Asp.net应用配置
   < appSettings >
    
< add  key ="SessionSourceType"  value ="http" />
    
< add  key ="HttpSessionSourceItemName"  value ="NHSession" />
    
< add  key ="UserSessionSource"  value ="true" />
  
appSettings >

  
< httpModules >
    
< add  type ="Index.Data.NHibernateData.SessionStorage.NHSessionModule, Index.Data.NHibernate"  name ="NHSessionModule" />
  
httpModules >
然后在程序开始的时候配置下
       Index.Data.NHibernateData.SessionStorage.NHibernateFactory.Configuration.Configure(Server.MapPath( " ~ " +   " //hibernate.cfg.xml " );

对于Asp.net我会把次段代码写在Global.asax的Application_Start方法里。 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部