NHibernate

(1) 基于.Net 的针对关系型数据库的对象持久化类库

(2) 面向.NET环境的对象/关系数据库映射工具

(3) 轻量级的ORM(Object/Relation Mapping)对象关系映射

使用:

(1) 创建项目:创建解决方案,添加类库

(2) 安装:再Nuget安装包里边搜索NHibernate 安装到DAL和UI类库中

(3) 配置数据库连接:将项目中ShoppackagesNHibernate.5.2.5ConfigurationTemplates目录下的所对应的***.cfg.xml 文件添加到UI项目中,并配置数据库连接字符串

<?xml version="1.0" encoding="utf-8"?>
<!-- 
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it 
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.dll provider for SQL Server -->
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory name="NHibernate.Test">
        <property name="connection.driver_class">NHibernate.Driver.Sql2008ClientDriver</property>
        <property name="connection.connection_string">
      Data Source=.;Initial Catalog=Shop;User ID=sa;password=sasa
    </property>
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    <!--在控制台显示SQL-->
    <property name="show_sql">true</property>
    <!--指定映射文档中所在程序集-->
    <mapping  assembly="Shop.Domain"/>
    </session-factory>
</hibernate-configuration>
Hibernate.cfg.xml

(4) 创建实体实体的属性必须是虚拟的: public virtual int CustomerID { get; set; }

public class Customers
    {
        public virtual int CustomerID { get; set; }
        public virtual string CompanyName { get; set; }
        public virtual string ContactName { get; set; }
        public virtual string ContactTitle { get; set; }
        public virtual string Address { get; set; }
        public virtual string City { get; set; }
        public virtual string Region { get; set; }
        public virtual string PostalCode { get; set; }
        public virtual string Country { get; set; }
        public virtual string Phone { get; set; }
        public virtual string Fax { get; set; }

    }
Customers

(5) 创建实体映射的XML:在实体类库新建-----数据----xml文件---*******.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Shop.DoMain"  namespace="Shop.DoMain.Entity">
  <class name="Customers" table="Customers">
    <id name="CustomerID" column="CustomerID" type="int"></id>
    <property name="CompanyName" column="CompanyName" type="string"></property>
    <property name="ContactName" column="ContactName" type="string"></property>
    <property name="ContactTitle" column="ContactTitle" type="string"></property>
    <property name="Address" column="Address" type="string"></property>
    <property name="City" column="City" type="string"></property>
    <property name="Region" column="Region" type="string"></property>
    <property name="PostalCode" column="PostalCode" type="string"></property>
    <property name="Country" column="Country" type="string"></property>
    <property name="Phone" column="Phone" type="string"></property>
    <property name="Fax" column="Fax" type="string"></property>
  </class>
</hibernate-mapping>
Customers.hbm.xml

(6)创建连接数据库工厂:

public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        public static ISessionFactory SessionFactory
        {
            get
            {
                //配置ISessionFactory
                return _sessionFactory == null ? (new Configuration()).Configure().BuildSessionFactory() : _sessionFactory;
            }
        }
    }
NHibernateHelper

(7) 创建常用泛型方法:

public class IBaseDAL<T> where T:class
    {           
        public T GetByID(int id)
        {
            using (ISession session = NHibernateHelper.SessionFactory.OpenSession())
            {
                T customers = session.Get<T>(id);
                return customers;
            }
        }
        public bool Insert(T t)
        {
            using (ISession session = NHibernateHelper.SessionFactory.OpenSession())
            {
                var identifier = session.Save(t);
                session.Flush();
                return string.IsNullOrEmpty(identifier.ToString());
            }
        }

        public void Update(T t)
        {
            using (ISession session = NHibernateHelper.SessionFactory.OpenSession())
            {
                session.SaveOrUpdate(t);
                session.Flush();
            }
        }
        public void Delete(int id)
        {
            using (ISession session = NHibernateHelper.SessionFactory.OpenSession())
            {
                T t = session.Get<T>(id);
                session.Delete(t);
                session.Flush();
            }
        }
    }
IBaseDAL

优势

(1) 使用时只需要操作对象,完全的面向对象思想

(2) 提供一,二级缓存和查询缓存

(3) 提升开发效率

(4) 支持多种数据库,便于数据库的迁移

(5) 透明持久化:

缺点

(1) 内存消耗使用NHibernate后,内存开销比较大

(2) 批量修改、删除数据,不适合用NHiberante

(3) 较多使用数据库特性时,也不适合使用NHiberante

(4) 表关系比较混乱时也不适合使用NHiberante。NHibernate只适合于表与表的关系比较明确的环境中

(5) 使用NHibernate需要有一定OOP(面向对象编程)和OOD(面向对象设计)的基础

原文地址:https://www.cnblogs.com/JueXiaoQiang/p/10819449.html