MVC Note (4) In NHibernate Part 2. 建立資料存取相關類別
在 Part 1. 已經建立我們要做為映射檔的 XML,接下來要開始實作一些需要用到的類別,而以下的類別皆建立在 Models 資料夾內。
Step 1. NHibernateHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate.Cfg;
using NHibernate;
namespace MvcApplication1.Models
{
public class NHibernateHelper
{
private static ISessionFactory _sessionfactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionfactory == null)
{
var configuraction = new Configuration();
configuraction.Configure();
_sessionfactory = configuraction.BuildSessionFactory();
}
return _sessionfactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
Step 1.1 hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the System.Data.dll provider for SQL Server -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Server=.\SQLEXPRESS;initial catalog=MVCNHibernate;User ID=sa;Password=123456;Integrated Security=SSPI
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="MvcApplication1"/>
</session-factory>
</hibernate-configuration>
* 這個設定檔直接放在專案底下,跟 Web.config 同一層。
* 在 Part 1. 所下載的套件內有一個 Configuration_Templates 的資料夾,有幾個常見的連線設定檔,可以依自己的需要直接複製過來並且重新
命名 hibernate.cfg,一定要命名 hibernate.cfg 要不然會發生無法連線的錯誤。
* 請注意 mapping assembly 設定的名稱,如果是類別庫,請設定為類別庫的輸出名稱。
Step 2. Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public class Person
{
public Guid ID { get; set; }
public String Name { get; set; }
public int Sex { get; set; }
public String Address { get; set; }
}
}
* 要注意屬性名稱須跟 Person.hbm.xml 的 Property Name 相符。
Step 3. IRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MvcApplication1.Models
{
/// <summary>
/// 泛型介面
/// </summary>
/// <typeparam name="T">泛型類別</typeparam>
public interface IRepository<T>
{
// 儲存
void Save(T entity);
// 更新
void Update(T entity);
//刪除
void Delete(T entity);
//取得單筆資料
T GetDataByID(Guid id);
//取得所有資料
IList<T> GetAll();
}
}
* 因為此次存取的對象只有一個,所以就沒有定義太多的方法,只定義基礎的方法而已。
Step 4. PersonRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using NHibernate.Criterion;
namespace MvcApplication1.Models
{
//明確實作 IRepository 介面
public class PersonRepository : IRepository<Person>
{
void IRepository<Person>.Save(Person entity)
{
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(entity);
transaction.Commit();
}
}
}
void IRepository<Person>.Update(Person entity)
{
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Update(entity);
transaction.Commit();
}
}
}
void IRepository<Person>.Delete(Person entity)
{
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Delete(entity);
transaction.Commit();
}
}
}
Person IRepository<Person>.GetDataByID(Guid id)
{
using (ISession session = NHibernateHelper.OpenSession())
{
return session.CreateCriteria<Person>().Add(Restrictions.Eq("ID", id)).UniqueResult<Person>();
}
}
IList<Person> IRepository<Person>.GetAll()
{
using (ISession session = NHibernateHelper.OpenSession())
{
return session.CreateCriteria<Person>().List<Person>();
}
}
}
}
* 繼承 IRepository 並明確實作各方法,T 指定 Person。
* 引用 NHibernate 及 NHibernate.Criterion 兩個命名空間。
以上幾個類別及設定檔建立好,對於資料存取控制的工作大致上都準備好了。接下來的 Part 3 就會講解開運用在 Controller 跟 View 上面
===================
大家好 , 我叫芋宅宅
我很菜 , 請各位前輩指教