⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nhibernatedataaccessor.cs

📁 该文件是多层体系结构的数据访问层
💻 CS
字号:
#region Copyright & License
//
// $Copyright:$
//
#endregion
#region Document Information
// $Author: Bai.shijun $
// $Rev: 511 $ 
// $Date: 05-10-08 16:13 $
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.EnterpriseServices;
using System.Diagnostics;

using NHibernate;

namespace Eai.Data
{
    public class NHibernateDataAccessor : IDataAccessor, IDisposable
    {
        private NHibernate.Cfg.Configuration nhConfig;
        private Eai.Data.FlushMode flushMode;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nhConfig"></param>
        /// <param name="flushMode">Flushģʽ.</param>
        public NHibernateDataAccessor(NHibernate.Cfg.Configuration nhConfig, Eai.Data.FlushMode flushMode)
        {
            Debug.Assert(nhConfig != null);

            this.nhConfig = nhConfig;
            this.flushMode = flushMode;
        }

        private ISession session;
        internal ISession Session
        {
            get
            {
                if (session == null)
                {
                    ISessionFactory factory = nhConfig.BuildSessionFactory();
                    session = factory.OpenSession();
                }
                return session;
            }
        }

        #region IDataAccessor Members

        public object Get(Type type, object id)
        {
            object obj = Session.Get(type, id);

            return obj;
        }

        public void Create(object entity)
        {
            Session.Save(entity);
            CheckFlush();
        }

        public void Update(object entity)
        {
            Session.Update(entity);
            CheckFlush();
        }

        public void Delete(object entity)
        {
            Session.Delete(entity);
        }

        public int Delete(string query)
        {
            int deleted = Session.Delete(query);
            return deleted;
        }

        public IList Query(string query, params object[] @params)
        {
            IQuery q = Session.CreateQuery(query);
            if (@params != null)
            {
                for (int i = 0; i < @params.Length; i++)
                {
                    q.SetParameter(i, @params[i]);
                }
            }

            return q.List();
        }

        public void Open()
        {
        }

        public bool IsOpen
        {
            get { return Session.IsOpen; }
        }

        public void Close()
        {
            Session.Close();
        }

        public void Flush()
        {
            Session.Flush();
        }

        #endregion

        private void CheckFlush()
        {
            if (flushMode == Eai.Data.FlushMode.AtOnce) Flush();
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (session != null && session.IsOpen)
            {
                session.Close();
                session = null;
            }
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -