给出几个问题总能让你带着思考去看这个文档,从中也许能够获得一些好处。
1.NHibernate是什么?
NHibernate是一个面向.NET环境的对象/关系数据库映射框架(工具)。对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去 。
如下图所示:
O:Object也就是class类,用于业务处理时的Domain类。如上图中的Customer类
R:就是数据库中的关系,也就是一张表。如上图中的Customer表。
M:Mapping File。是将Customer类中的属性和Customer表中的字段进行对应的映射文件,它起到了很重要的作用
通过Mapping File就可以把Customer类中的属性和Customer表中的字段进行映射。对于开发者通常的数据持久化相关的编程任务,解放其中的95%!那么Mapping File又是怎么工作的呢?这就得说道NHibernate这个框架的强大之处了。NHibernate框架会根据NHibernate.cfg.xml文件的配置找到这个Mapping File,然后由框架自动的根据这个文件来将实体类和关系表作映射。那你又要问什么是NHibernate.cfg.xml是什么?听我给你慢慢说。
NHibernate.cfg.xml这个文件时用来配置数据库的基本信息和configure和SessionFactory实例的基本信息.
具体请参看另一篇文章http://4837471.blog.51cto.com/4827471/1564224
2.NHibernate能干什么?
其实在上面我就说了:对于开发者通常的数据持久化相关的编程任务,解放其中的95%!简化了编程人员的工作。让对数据持久化操作更加简便。
3.NHibernate如何使用?
下面我将拿出一个自己做的Demo进行说明如下:
VS2013+SQLServer2008+NHibernate2.2
第一步:
1.1:创建一个类库项目命名为NHibernateDLL。
1.2:编写一个Student类:代码如下
可以借助Entity Developer工具连接数据库进行生成。
//------------------------------------------------------------------------------
// This is auto-generated code.
//------------------------------------------------------------------------------
// This code was generated by Entity Developer tool using NHibernate template.
// Code is generated on: 2014/10/14 18:47:41
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace NHibernateDLL
{
/// <summary>
/// There are no comments for Student in the schema.
/// </summary>
public partial class Student {
#region Extensibility Method Definitions
/// <summary>
/// There are no comments for OnCreated in the schema.
/// </summary>
partial void OnCreated();
#endregion
/// <summary>
/// There are no comments for Student constructor in the schema.
/// </summary>
public Student()
{
this.RegisterTime = DateTime.Now;
OnCreated();
}
/// <summary>
/// There are no comments for Stuno in the schema.
/// </summary>
public virtual int Stuno
{
get;
set;
}
/// <summary>
/// There are no comments for Name in the schema.
/// </summary>
public virtual string Name
{
get;
set;
}
/// <summary>
/// There are no comments for Sex in the schema.
/// </summary>
public virtual string Sex
{
get;
set;
}
/// <summary>
/// There are no comments for Age in the schema.
/// </summary>
public virtual int Age
{
get;
set;
}
/// <summary>
/// There are no comments for Phone in the schema.
/// </summary>
public virtual string Phone
{
get;
set;
}
/// <summary>
/// There are no comments for Addr in the schema.
/// </summary>
public virtual string Addr
{
get;
set;
}
/// <summary>
/// There are no comments for RegisterTime in the schema.
/// </summary>
public virtual System.Nullable<System.DateTime> RegisterTime
{
get;
set;
}
}
}1.3:编写Mapping文件代码如下:(将Mapping文件设置为嵌入的资源文件)也可借助工具生成
<?xml version="1.0" encoding="utf-8"?> <hibernate-mapping assembly="NHibernateDLL" namespace="NHibernateDLL" xmlns="urn:nhibernate-mapping-2.2"> <class name="Student" table="Student" schema="dbo"> <id name="Stuno" type="Int32"> <column name="Stuno" not-null="true" precision="10" scale="0" sql-type="int" /> <generator class="assigned" /> </id> <property name="Name" type="String"> <column name="Name" not-null="true" length="20" sql-type="char" /> </property> <property name="Sex" type="String"> <column name="Sex" not-null="true" length="2" sql-type="char" /> </property> <property name="Age" type="Int32"> <column name="Age" not-null="true" precision="10" scale="0" sql-type="int" /> </property> <property name="Phone" type="String"> <column name="Phone" not-null="false" length="16" sql-type="varchar" /> </property> <property name="Addr" type="String"> <column name="Addr" not-null="false" length="50" sql-type="varchar" /> </property> <property name="RegisterTime" type="DateTime"> <column name="Register_Time" default="getdate()" not-null="false" sql-type="datetime" /> </property> </class> </hibernate-mapping>
1.4:生成dll文件
第二步:
2.1:在同一个方案下创建一个类库项目:命名为DataAccessLayer
2.2:创建一个类,命名为NHibernateDataProvider
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Cfg;
using NHibernate;
using NHibernateDLL;
//同时添加相应的DLL
namespace DataAccessLayer
{
public class NHibernateDataProvider
{
public ISession _session;
public NHibernateDataProvider(ISession session)
{
this._session = session;
}
/// <summary>
/// 通过学号Key来获得学生实体
/// </summary>
/// <param name="key">学号</param>
/// <returns>学生实体</returns>
public Student GetStudentById(int key)
{
return (NHibernateDLL.Student) _session.Get<Student>(key);
}
}
} 2.3:生成类库dll文件
第三步:测试
3.1:新建单元测试项目
3.2:添加引用
3.3:添加NHibernate.cfg.xml文件
<?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="sessionFactory"> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string">Data Source=127.0.0.1;Initial Catalog=Student;Persist Security Info=True;User ID=sa;Password=123456</property> <property name="show_sql">true</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <mapping assembly="DataAccessLayer"/> </session-factory> </hibernate-configuration>
3.4:编写测试方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MbUnit.Framework;
using DataAccessLayer;
using NHibernateDLL;
using NHibernate;
namespace TestForStudentModel
{
[TestFixture]
public class UnitTest1:Microdesk.Utility.UnitTest.DatabaseUnitTestBase
{
private NHibernateDataProvider _provider;
private ISession _session;
[Test]
public void TestGetStudentById()
{
Student student = _provider.GetStudentById(1);
Console.WriteLine(student.Name+" "+student.Stuno+" "+student.RegisterTime);
}
}
}4.NHibernate的好处是什么?
由上面的例子我们就很清楚的看到,他让编程更加简单了,采用了对象化的数据访问封装,让其看起来更加的牛逼了,不用写一条sql语句就能够获得数据库中的数据,这就让编程人员从数据库中抽身出来,专注于业务逻辑的实现,而数据持久化层的管理就交给这个NHibernate管理就好了,是不是感觉很不错的,确实让编码变得简单起来了。
你还在等什么?赶紧自己去尝试一下吧!
本文出自 “爱咖啡” 博客,请务必保留此出处http://4837471.blog.51cto.com/4827471/1564352
原文:http://4837471.blog.51cto.com/4827471/1564352