var blog = new ThoughtStream(me); RSS 2.0
 Monday, March 24, 2008

This might be common knowledge for those that use NHibernate - but it's new to me, since I am not an NHibernate power-user, yet.

Given this object model:

public class Invoice
{
    private int _id = 0;
    private string _invoiceNumber = string.Empty;
    private DateTime _invoiceDueDate = DateTime.MinValue;
 
    private ICollection<InvoiceDetail> _invoiceDetails = new HashedSet<InvoiceDetail>();
 
    public int Id
    {
        get { return _id; }
    }
 
    public string InvoiceNumber
    {
        get { return _invoiceNumber; }
        set { _invoiceNumber = value; }
    }
 
    public DateTime InvoiceDueDate
    {
        get { return _invoiceDueDate; }
        set { _invoiceDueDate = value; }
    }
 
    public ICollection<InvoiceDetail> InvoiceDetails
    {
        get { return _invoiceDetails; }
    }
 
    public InvoiceDetail CreateDetail()
    {
        InvoiceDetail detail = new InvoiceDetail();
        InvoiceDetails.Add(detail);
        return detail;
    }
 
}
 
 
public class InvoiceDetail
{
 
    internal InvoiceDetail() { }
 
    private int _id = 0;
    private string _productName = string.Empty;
    private decimal _productCost = 0;
    private int _productQuantity = 0;
 
    public int Id
    {
        get { return _id; }
    }
 
    public string ProductName
    {
        get { return _productName; }
        set { _productName = value; }
    }
 
    public decimal ProductCost
    {
        get { return _productCost; }
        set { _productCost = value; }
    }
 
    public int ProductQuantity
    {
        get { return _productQuantity; }
        set { _productQuantity = value; }
    }
 
}

We can load any invoice that has an Invoice Detail with a specific Product Name, using this NHibernate code:

invoices = Session.CreateCriteria(typeof(Invoice))
    .CreateCriteria("InvoiceDetails")
    .Add(Expression.Eq("ProductName", productName))
    .List<Invoice>();

Additionally, we can add paging to the result set, by including the SetFirstResult and SetMaxResult calls in the Criteria.

invoices = Session.CreateCriteria(typeof(Invoice))
    .CreateCriteria("InvoiceDetails")
    .Add(Expression.Eq("ProductName", productName))
 
    .SetFirstResult(pageSize * pageNumber)
    .SetMaxResults(pageSize - 1)
 
    .List<Invoice>();

Notice the use of pageSize and PageNumber to set the First Result value - this creates an index-by-zero page number that we want to view. For example, if our page size is 10 and we are on page zero, then the FirstResult is going to be 0 and the last result will be 9. 0 through 9 = 10 results. If we are on page 3, then the FirstResult is 30 and the MaxResult is 39...

Works pretty well... now, let's just hope that the actual underlying database implementation of this can take advantage of each DBMS' optimizations for doing this.

Monday, March 24, 2008 4:39:52 PM (Central Standard Time, UTC-06:00)  #    Comments [0]. Trackback 
Tags: .NET | Data Access | NHibernate

Comments are closed.
Navigation
About Me
View Derick Bailey's profile on LinkedIn

Send mail to the author(s) Contact Me
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Derick Bailey
Sign In
Statistics
Total Posts: 115
This Year: 0
This Month: 0
This Week: 0
Comments: 44
Themes
Pick a theme:
All Content © 2012, Derick Bailey
DasBlog theme 'Business' created by Christoph De Baene (delarou)