在数据库操作中,我们除了对表操作,还有视图、存储过程等操作,这一篇和下篇来学习这些内容。这篇我们来学习如何在NHibernate中使用视图。首先,我们在数据库中建立一个名为viewCustomer视图,选中CustomerId、Firstname、Lastname、OrderId、OrderDate项。øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
下面我们依次为这个视图编写持久化类和映射吧。øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
1.持久化类同持久化数据库中的表类似,我们需要对视图持久化,定义视图中的每个属性,因为视图是只读的,所以在这里我们只要把属性的Setter设置为private访问权限。具体做法如下:øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
在项目DomainModel层的Entities文件夹中新建CustomerView.cs类,编写代码如下:øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
namespace DomainModel.EntitiesøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
{øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    public class CustomerViewøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    {øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
        public virtual int CustomerId { get; private set; }øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
        public virtual string Firstname { get; private set; }øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
        public virtual string Lastname { get; private set; }øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
        public virtual int OrderId { get; private set; }øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
        public virtual DateTime OrderDate { get; private set; }øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    }øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}2.映射文件在项目DomainModel层的Mappings文件夹中新建CustomerView.hbm.xml文件,与映射数据库表类似,编写代码如下:øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
<?xml version="1.0" encoding="utf-8" ?>øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
<
hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
                  assembly="DomainModel" namespace="DomainModel">øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
  øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
  <
class name="DomainModel.Entities.CustomerView,DomainModel" øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
        table="viewCustomer" mutable="false" >øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    <
id name="CustomerId" column="CustomerId" type="Int32">øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
      <
generator class="native" />øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    </
id>øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    <
property name="Firstname" column="Firstname" type="string" />øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    <
property name="Lastname" column="Lastname" type="string" />øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    <
property name="OrderId" column="OrderId" type="Int32" />øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    <
property name="OrderDate" column="OrderDate" type="DateTime" />øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
  </
class>øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
</
hibernate-mapping>øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
好了,到这里我们准备工作就做完了,即完成了持久化和映射。下面我们可以使用视图了。øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
3.测试在数据访问层(DAL)中编写一个方法获取订单时间在orderDate之后的顾客列表,方法如下:øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
public IList<CustomerView> GetCustomerView(DateTime orderDate)øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
{øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    return _session.CreateCriteria(typeof(CustomerView))øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
        .Add(Restrictions.Gt("OrderDate", orderDate))øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
        .List<CustomerView>();øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}在数据访问测试层(DAL.Test)中编写一个方法由于测试上面的方法。首先调用这个方法查询出订单时间在2008年10月1日之后的顾客列表,断言其订单时间是否大于2008年10月1日。øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
[Test]øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
public void GetCustomerViewTest()øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
{øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    DateTime testorderDate = new DateTime(2008, 10, 1);øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    IList<CustomerView> customers =øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
        _relation.GetCustomerView(testorderDate);øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    foreach (CustomerView view in customers)øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    {øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
        Assert.GreaterOrEqual(view.OrderDate, testorderDate);øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
    }øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}OK!测试通过,NHibernate生成SQL语句如下:øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
SELECT this_.CustomerId as CustomerId0_0_,øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
     
this_.Firstname as Firstname0_0_,øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
     
this_.Lastname as Lastname0_0_,øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
     
this_.OrderId as OrderId0_0_,øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
     
this_.OrderDate as OrderDate0_0_øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
FROM viewCustomer this_ øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
WHERE this_.OrderDate > @p0; @p0 = '2008/10/1 0:00:00'好了,到此我们学会了在NHibernate中如何使用视图,是不是很简单啊。øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
结语通过这篇文章的展示,我们学习了在NHibernate中如何使用视图,同表类似,只是属性访问权限不同罢了,如果你原来不知道如何使用视图,网上到现在也没有相关资料觉得无从下手,通过这篇文章的快速阅读,是不是使用视图非常简单,豁然开朗的样子(视图原来这样啊,没有什么神秘之处~~)。下篇我们来看看NHibernate中使用储存过程,用过存储过程的朋友都知道,真是烦人,这个存储过程我真是弄了很长时间,在实际运用中错误不断,我把它一一化解,请征集意见,大家说下篇是写一一化解的整个详细过程(涉及错误信息,如何修改,2篇样子)还是直接讲正确方案(1篇搞定)。由你做主!øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø