NHibernate Tour (14): Explore using Views in NHibernate

Source: Internet
Author: User

The content of this section

    • Introduced
    • 1. Persistence class
    • 2. mapping files
    • 3. Testing
    • Conclusion
Introduced

In the database operation, we can learn these things in addition to the operation of the table, the view, the stored procedure, and the next article. In this article we will learn how to use views in nhibernate. First, we create a view in the database named Viewcustomer, with the CustomerID, Firstname, Lastname, OrderId, and OrderDate items selected.

Let's write the Persistence class and mapping for this view in turn.

1. Persistence class

Similar to a table in a persisted database, we need to persist the view to define each property in the views, because the view is read-only, so here we just set the setter of the property to private access. The following are the specific practices:

To create a new CustomerView.cs class in the Entities folder of the project Domainmodel layer, write the following code:

namespacedomainmodel.entities{Public classCustomerview{Public Virtual intCustomerId {Get;Private Set; }Public Virtual StringFirstname {Get;Private Set; }Public Virtual StringLastname {Get;Private Set; }Public Virtual intOrderId {Get;Private Set; }Public VirtualDateTimeOrderDate {Get;Private Set; }    }}
2. mapping files

Create a new CustomerView.hbm.xml file in the Mappings folder in the project Domainmodel layer, similar to the Mapping database table, and write the following code:

<?XMLversion="1.0"encoding="Utf-8"?><hibernate-mappingxmlns="urn:nhibernate-mapping-2.2"Assembly="Domainmodel"namespace="Domainmodel"> <classname="Domainmodel.entities.customerview,domainmodel"Table="Viewcustomer"mutable="false"> <IDname="CustomerId"column="CustomerId"type="Int32"> <Generatorclass="native"/> </ID> < Propertyname="Firstname"column="Firstname"type="string"/> < Propertyname="Lastname"column="Lastname"type="string"/> < Propertyname="OrderId"column="OrderId"type="Int32"/> < Propertyname="OrderDate"column="OrderDate"type="DateTime"/> </class></hibernate-mapping>

Well, when we get here, we're done, and we've finished the persistence and mapping. We can use the view below.

3. Testing

Write a method in the data Access Layer (DAL) to get a list of customers after the order time in OrderDate, as follows:

public   IList  <customerview  > Getcustomerview (datetime  orderDate" {return  _session. Createcriteria (typeof  (customerview ) )        . ADD (restrictions . Gt ( "OrderDate" , OrderDate)). List<customerview  > ();} 

In the Data Access test layer (DAL. Test), write a method because the above method is tested. Call this method first to query the list of customers whose order time is after October 1, 2008, and to assert whether the order time is greater than October 1, 2008.

[Test]Public voidGetcustomerviewtest () {DateTimeTestorderdate =NewDateTime(2008, 10, 1);IList<Customerview> Customers = _relation. Getcustomerview (testorderdate);foreach(CustomerviewViewinchCustomers) {Assert. Greaterorequal (view.    OrderDate, testorderdate); }}

Ok! The test passes, and the nhibernate generates SQL statements as follows:

SELECTThis_.CustomerId ascustomerid0_0_,       This_.Firstname asfirstname0_0_,       This_.Lastname aslastname0_0_,       This_.OrderId asorderid0_0_,       This_.OrderDate asorderdate0_0_ fromViewcustomer This_WHEREThis_.OrderDate>@p0; @p0= ' 2008/10/1 0:00:00 '

Well, here we have learned how to use the view in NHibernate, is not very simple AH.

Conclusion

Through the presentation of this article, we learned how to use the view in the NHibernate, similar to the table, only the property access permissions are different, if you originally do not know how to use the view, online to now there is no relevant data feel impossible, through the quick reading of this article, is not the use of the view is very simple, The appearance of the enlightened (view original so Ah, there is no mystery of the place ~ ~). Next we look at the use of stored procedures in the NHibernate, with the stored procedures of friends know, it is annoying, this stored procedure I really got a long time, in the actual use of the wrong constantly, I will dissolve it all, please call for comments, we say that the whole process of resolving the entire detail (involving error information, How to modify, 2) or directly say the right plan (1). You're the Boss!

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

NHibernate Tour (14): Explore using Views in NHibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.