首先,下载Nhibernate3.3.3 GA(http://sourceforge.net/projects/nhibernate/)
拿到bins下的文件:
新建项目,结构如下:
说明:
NHibernate.DataPortal是数据门户,
NHibernate.Domain是Model层,
NHibernateUI是界面层,典型的三层架构
对相关文件的解释:
NHibernate.Domain中,Entities文件夹内的Customer.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NHibernate.Domain.Entities { [Serializable] public class Customer { #region Customer public virtual string 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; } #endregion } }
针对Customer的配置文件Customer.hbm.xml,同时把文件的属性的默认生成操作为“内容”,这里需要修改为“嵌入的资源”生成,因为NHibernate是通过查找程序集中的资源文件映射实体
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Domain" namespace="NHibernate.Domain.Entities"> <class name="NHibernate.Domain.Entities.Customer,NHibernate.Domain" table="Customers"> <id name="CustomerID" column="CustomerID" type="string" unsaved-value="0"> <generator class="increment"/> </id> <property name="CompanyName" column="CompanyName" type="string" /> <property name="ContactName" column="ContactName" type="string" /> <property name="ContactTitle" column="ContactTitle" type="string" /> <property name="Address" column="Address" type="string" /> <property name="City" column="City" type="string" /> <property name="Region" column="Region" type="string" /> <property name="PostalCode" column="PostalCode" type="string" /> <property name="Country" column="Country" type="string" /> <property name="Phone" column="Phone" type="string" /> <property name="Fax" column="Fax" type="string" /> </class> </hibernate-mapping>
NHibernate.DataPortal中NHibernateHelper类代码:
using NHibernate.Cfg; namespace NHibernate.DataPortal { public class NHibernateHelper { private ISessionFactory _sessionFactory; public NHibernateHelper() { _sessionFactory = GetSessionFactory(); } private ISessionFactory GetSessionFactory() { return (new Configuration()).Configure().BuildSessionFactory(); } public ISession GetSession() { return _sessionFactory.OpenSession(); } } }
NHibernate.DataPortal中CustomerDal类代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NHibernate.Domain.Entities; namespace NHibernate.DataPortal { public class CustomerDal { private NHibernateHelper nhibernateHelper = new NHibernateHelper(); protected ISession Session { get; set; } public CustomerDal() { this.Session = nhibernateHelper.GetSession(); } public CustomerDal(ISession session) { this.Session = session; } public void CreateCustomer(Customer customer) { Session.Save(customer); Session.Flush(); } public Customer GetCustomerById(int customerId) { return Session.Get<Customer>(customerId); } public IList<Customer> GetCunstomers() { IList<Customer> list = null; list = Session.QueryOver<Customer>().List(); return list; } } }
关键点,与数据库打交道的配置文件hibernate.cfg.xml,把文件的默认“复制到输出目录”为“不复制”,这里需要修改为“始终复制”
<?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string">Server=.;initial catalog=northwind;Integrated Security=True</property> <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory,NHibernate</property> <property name="show_sql">true</property> <property name="command_timeout">10</property> <property name="adonet.batch_size">10</property> <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> <mapping assembly="NHibernate.Domain" /> </session-factory> </hibernate-configuration>
NHibernateUI层窗体的调用
protected override void OnLoad(EventArgs e) { try { NHibernate.DataPortal.CustomerDal customerDal = new NHibernate.DataPortal.CustomerDal(); this.dgvCustomerList.DataSource = customerDal.GetCunstomers(); } catch (Exception ex) { throw new Exception(ex.Message); } }
最后显示结果
在这次使用过程中,发生了很多的问题,但是因为NHibernate 3.3.3版本刚出来不久,使用的案例比较少,特别是很多配置的地方特别不一样,所以费了很多的周折,写给自己备用,也希望给朋友你带来一些帮助