NHibernate 3.3.3GA

首先,下载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版本刚出来不久,使用的案例比较少,特别是很多配置的地方特别不一样,所以费了很多的周折,写给自己备用,也希望给朋友你带来一些帮助


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值