解决方案资源预览:
在NHibernate.Domain中新建实体类和映射类如下:
<?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>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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
}
}
在NHibernate.DataPortal中添加NHibernateHelper.cs,代码如下:
using NHibernate.Cfg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.cs类如下:
using NHibernate.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
}
public IList<Customer> GetCunstomersByID(string customerId)
{
IList<Customer> list = null;
list = Session.QueryOver<Customer>().AndRestrictionOn(p => p.CustomerID).IsLike(customerId, NHibernate.Criterion.MatchMode.Anywhere).List();
return list;
}
}
}
在NHibernateUI项目中form1设计界面如下:
设计form1的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NHibernate.DataPortal;
namespace NHibernateUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
NHibernate.DataPortal.CustomerDal customerDal = new NHibernate.DataPortal.CustomerDal();
this.dataGridView1.DataSource = customerDal.GetCunstomers();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
NHibernate.DataPortal.CustomerDal customerDal = new NHibernate.DataPortal.CustomerDal();
this.dataGridView1.DataSource = customerDal.GetCunstomersByID(textBox1.Text);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
整体功能完成,本例子主要联系NHibernate的配置和使用。注意SessionFactory和Session的使用方法。