標籤:nhibernate demo object 開發人員 技術 模型
給出幾個問題總能讓你帶著思考去看這個文檔,從中也許能夠獲得一些好處。
1.NHibernate是什嗎?
NHibernate是一個面向.NET環境的對象/關聯式資料庫映射架構(工具)。對象/關聯式資料庫映射(object/relational mapping,ORM)這個術語表示一種技術,用來把物件模型表示的對象映射到基於SQL的關聯式模式資料結構中去 。
如所示:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/4C/74/wKioL1Q90SHzakCCAAECLsvc92E846.jpg" title="1.png" alt="wKioL1Q90SHzakCCAAECLsvc92E846.jpg" />
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。
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/4C/76/wKiom1Q96bmxS49IAACyK69X2gE331.jpg" title="捕獲.PNG" alt="wKiom1Q96bmxS49IAACyK69X2gE331.jpg" />
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檔案
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4C/77/wKioL1Q96szQxxMzAAArWNCDjpM628.jpg" title="捕獲.PNG" alt="wKioL1Q96szQxxMzAAArWNCDjpM628.jpg" />
第二步:
2.1:在同一個方案下建立一個類庫項目:命名為DataAccessLayer
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/4C/78/wKioL1Q-CqKANpe_AABrsisPlVI395.jpg" title="捕獲.PNG" alt="wKioL1Q-CqKANpe_AABrsisPlVI395.jpg" />
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;//同時添加相應的DLLnamespace 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:建立單元測試項目
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4C/77/wKiom1Q-DSrzzUcqAACWx_G4uXo237.jpg" title="捕獲.PNG" alt="wKiom1Q-DSrzzUcqAACWx_G4uXo237.jpg" />
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
NHibernate學習總結