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. Test
    • 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'll learn how to use views in nhibernate. First, we create a view in the database named Viewcustomer. Select CustomerID, Firstname, Lastname, OrderId, and OrderDate items.

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 simply set the setter of the property to private access.

The detailed procedure is for example the following:

Create a new CustomerView.cs class in the entities directory of the project Domainmodel layer, and write code such as the following:

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 directory of the project Domainmodel layer. Similar to mapping database tables. Write code such as the following:

<?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>

All right, we're done here when we get ready for work. Persistence and mapping is complete.

Below we can use the view.

3. Test

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

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

In the Data interview test layer (DAL. Test), write a method because you test the method above. This method is called first to query the list of customers who have orders after October 1, 2008. Asserts whether its 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!

Test Pass. NHibernate generates SQL statements such as the following:

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 '

All right. Here we learned how to use the view in the NHibernate, is not very easy ah.

Conclusion

Through the presentation of this article, we learned how to use the view in the NHibernate, similar to the table, just the property access permission is different, assuming that you did not know how to use the view, online to now there is no relevant data think, through this article of high-speed reading, is not the use of the view is very easy, enlightened appearance (view original so Ah, there is no magic place ~ ~).

In the next section, we'll look at the use of stored procedures in NHibernate, known to all friends who have stored procedures. It's annoying, this stored procedure really took me a long time. In the actual use of the wrong constantly, I have to dissolve it, please call for comments, we say that the whole process of writing a solution (involving error information. How to change, 2) or directly say the right plan (1). You're the Boss!

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.