dinghao

记录成长点滴

 

操作Nhibernate

我认为好的学习Nhiberante的方法:
1了解HN的结构
2学习HN的项目,如:Cuyahoga
3看Hibernate的文档
4具体问题看NH的测试用例(我认为是最方便的方法,用例写的很全,也可以通过它学习NUnit),从这里也可以看到好的测试用例是最好的文档

下面是操作NH的通用类,来自Cuyahoga,有我的一些修改:
提供Session的类

using System;
using System.Reflection;

using NHibernate;
using NHibernate.Cfg;

namespace VirtualBank.Service
{
    
/// <summary>
    
/// The SessionFactory provides the NHibernate sessions and provides the possibility to register
    
/// additional classes with NHibernate by modules.
    
/// </summary>

    public class SessionFactory
    
{    
        
//是这个类
        private static SessionFactory _sessionFactory = new SessionFactory();

        
private Configuration _nhibernateConfiguration;
        
//SessionFactory是Hibernate的概念,对应一个数据存储源
        private ISessionFactory _nhibernateFactory;
        
private bool _classesAdded = false;

        
/// <summary>
        
/// Default constructor.
        
/// </summary>

        protected SessionFactory()
        
{
            RegisterCoreClasses();
            
        }


        
/// <summary>
        
/// Gets the one instance of the SessionFactory. This is done with a singleton so we don't have
        
/// to register mappings etc. with every request.
        
/// </summary>
        
/// <returns></returns>

        public static SessionFactory GetInstance()
        
{
            
return _sessionFactory;
        }


        
/// <summary>
        
/// GetNHibernateFactory returns the current NHibernate ISessionFactory.
        
/// </summary>

        public ISessionFactory GetNHibernateFactory()
        
{
            
return this._nhibernateFactory;
        }


        
/// <summary>
        
/// Get a new NHibernate session.
        
/// </summary>
        
/// <returns></returns>

        public ISession GetSession()
        
{
            
return this._nhibernateFactory.OpenSession();
        }


        
/// <summary>
        
/// Add a class to the NHibernate mappings.
        
/// If the class already is mapped, nothing will happen. 
        
/// </summary>
        
/// <param name="type"></param>

        public void RegisterPersistentClass(Type type)
        
{
            
if (this._nhibernateConfiguration.GetClassMapping(type) == null)
            
{
                
// Class isn't mapped yet, so do it now.
                this._nhibernateConfiguration.AddClass(type);
                
this._classesAdded = true;
            }
            
        }


        
/// <summary>
        
/// Rebuild the NHibernate ISessionFactory. Use it after registering new classes.
        
/// </summary>

        public bool Rebuild()
        
{
            
// Rebuild NHibernate SessionFactory to activate the new mapping.
            if (this._classesAdded)
            
{
                
this._nhibernateFactory = this._nhibernateConfiguration.BuildSessionFactory();
                
this._classesAdded = false;
                
return true;
            }

            
else
            
{
                
return false;
            }

        }


        
private void RegisterCoreClasses()
        
{
            Configuration config 
= new Configuration();
            
//??
            this._nhibernateConfiguration = config.AddAssembly(this.GetType().Assembly);
            
//应用程序为了得到ISession实例,必须先得到它的工厂。这个工厂应该是被应用程序的所有线程共享的: 

            
this._nhibernateFactory = this._nhibernateConfiguration.BuildSessionFactory();
        }

    }

}

具体操作的类,封装了一些通用方法

using System;
using System.Collections;
using System.Reflection;

using log4net;
using NHibernate;
using NHibernate.Expression;


namespace VirtualBank.Service
{
    
/// <summary>
    
/// This is the repository for persistance of the Cuyahoga core classes. Maybe it 
    
/// should be split up into several classes, but we'll start with one
    
/// repository for all core classes.
    
/// </summary>

    public class CoreRepository
    
{
        
private static readonly ILog log = LogManager.GetLogger(typeof(CoreRepository));

        
private ISessionFactory _factory;
        
private ISession _activeSession;
    

        
/// <summary>
        
/// Get the active NHibernate session.
        
/// </summary>

        public ISession ActiveSession
        
{
            
get return this._activeSession; }
        }

        
/// <summary>
        
/// Create a repository for core objects.
        
/// </summary>

        //TODO?语法
        public CoreRepository() : this(false)
        
{
        }


        
/// <summary>
        
/// Create a repository for core objects.
        
/// </summary>
        
/// <param name="openSession">Indicate if the CoreRepository should open a session and keep it in memory.</param>

        public CoreRepository(bool openSession)
        
{    
            
//看SessionFactory类
            this._factory = SessionFactory.GetInstance().GetNHibernateFactory();
            
if (openSession)
            
{
                
this._activeSession = this._factory.OpenSession();
            }

        }


        
/// <summary>
        
/// Open a NHibernate session.
        
/// </summary>

        public void OpenSession()
        
{
            
if (this._activeSession == null || ! this._activeSession.IsOpen)
            
{
                
this._activeSession = this._factory.OpenSession();
            }

            
else
            
{
                
throw new InvalidOperationException("The repository already has an open session");
            }

        }


        
/// <summary>
        
/// Flushes the current active NHibernate session.
        
/// </summary>

        public void FlushSession()
        
{
            
if (this._activeSession != null && this._activeSession.IsOpen)
            
{
                
this._activeSession.Flush();
            }

        }


        
/// <summary>
        
/// Close the active NHibernate session
        
/// </summary>

        public void CloseSession()
        
{
            
if (this._activeSession != null)
            
{
                
if (this._activeSession.IsOpen)
                
{
                    
this._activeSession.Close();
                }

                
//this._activeSession.Dispose();
            }

        }

        
新的方法
        
Generic methods

        


    
    }

}


 

posted on 2006-05-11 10:35  思无邪  阅读(596)  评论(0编辑  收藏  举报

导航