概述:
SystemConfigMVCDemo.DAO,SystemConfigMVCDemo.Bussiness,
SystemConfigMVCDemo.Service 封装了访问数据库的框架,然后该框架易于拓展,即在数据库表对象的增加,删除,修改时,简单修改框架就可以使用了。该三层使用了面向对象的设计,从而能够拓展。
详解:
(1)SystemConfigMVCDemo.DAO,如下是该动态链接库的类图:
HibernateAccess 类 有二个作用:1,管理Hibernate的Session对象。2,实现了对象的增加,删除,修改的方法。
HibernateDAO 类,实现了IBussinessDAO接口的几个方法,在HibernateDAO类中,包含了HibernateAccess对象,所以通过HibernateAccess对象来访问数据库。
SystemConfigDAO类,继承HibernateDAO 类,因为HibernateDAO 类中的方法是共有的,所以子类中可以访问。所以在SystemConfigDAO类的对象边可以使用HibernateDAO 类中的方法。还有一点,HibernateDAO类中的HibernateAccess类对象是protected型的,所以在SystemConfigDAO类中可以使用,以便来拓展自己的一些特殊的方法。
贴出几个类的简短代码:
HibernateAccess :
public class HibernateAccess : IDisposable
{
[ThreadStatic]
private static ISession m_Session;
protected static ISessionFactory m_SessionFactory;
#region Session管理
/// <summary>
/// 静态构造函数。
/// </summary>
static HibernateAccess()
{
Init();
}
/// <summary>
/// 初始化NHibernate对象。
/// </summary>
private static void Init()
{
try
{
#if test
sessionFactory = new Configuration().Configure(@"C:\conn1\hibernate.cfg.config").BuildSessionFactory();
#endif
#if first
string currrentPath=System.AppDomain.CurrentDomain.BaseDirectory;
m_SessionFactory = new Configuration().Configure(currrentPath + @"\hibernate.cfg.config").BuildSessionFactory();
#endif
#if second
sessionFactory = new Configuration().Configure("hibernate.cfg.xml").BuildSessionFactory();
#endif
}
catch (Exception exp)
{
throw exp;
}
}
#endregion
/// <summary>
/// 获得NHibernate的Session对象。
/// </summary>
public ISession Session
{
get
{
if (m_Session == null)
{
lock (SessionFactory)
{
m_Session = SessionFactory.OpenSession();
}
}
else
{
if (!m_Session.IsOpen)
m_Session.Reconnect();
}
return m_Session;
}
}
/// <summary>
/// 获取NHibernate的SessionFactory对象。
/// </summary>
public static ISessionFactory SessionFactory
{
get
{
return m_SessionFactory;
}
}
#region 增加,删除,修改,查询的基本方法
#region 查询
/// <summary>
/// 获取指定的对象。
/// </summary>
/// <param name="key">对象的关键字</param>
/// <param name="type">对象类型。</param>
/// <returns>如果对象存在则返回对象,否则返回Null。</returns>
public virtual object Find(object key, Type type)
{
return Session.Get(type, key);
}
#endgrion
#region 增加
#endregion
#region 删除
#endregion
#region 修改
#endregion
}
{
[ThreadStatic]
private static ISession m_Session;
protected static ISessionFactory m_SessionFactory;
#region Session管理
/// <summary>
/// 静态构造函数。
/// </summary>
static HibernateAccess()
{
Init();
}
/// <summary>
/// 初始化NHibernate对象。
/// </summary>
private static void Init()
{
try
{
#if test
sessionFactory = new Configuration().Configure(@"C:\conn1\hibernate.cfg.config").BuildSessionFactory();
#endif
#if first
string currrentPath=System.AppDomain.CurrentDomain.BaseDirectory;
m_SessionFactory = new Configuration().Configure(currrentPath + @"\hibernate.cfg.config").BuildSessionFactory();
#endif
#if second
sessionFactory = new Configuration().Configure("hibernate.cfg.xml").BuildSessionFactory();
#endif
}
catch (Exception exp)
{
throw exp;
}
}
#endregion
/// <summary>
/// 获得NHibernate的Session对象。
/// </summary>
public ISession Session
{
get
{
if (m_Session == null)
{
lock (SessionFactory)
{
m_Session = SessionFactory.OpenSession();
}
}
else
{
if (!m_Session.IsOpen)
m_Session.Reconnect();
}
return m_Session;
}
}
/// <summary>
/// 获取NHibernate的SessionFactory对象。
/// </summary>
public static ISessionFactory SessionFactory
{
get
{
return m_SessionFactory;
}
}
#region 增加,删除,修改,查询的基本方法
#region 查询
/// <summary>
/// 获取指定的对象。
/// </summary>
/// <param name="key">对象的关键字</param>
/// <param name="type">对象类型。</param>
/// <returns>如果对象存在则返回对象,否则返回Null。</returns>
public virtual object Find(object key, Type type)
{
return Session.Get(type, key);
}
#endgrion
#region 增加
#endregion
#region 删除
#endregion
#region 修改
#endregion
}