views:

286

answers:

1

I'm new to NHibernate and trying to create my first mapping.

I have created a class like this (my example is simplified):

public class Buyer
{
    public int BuyerID { get; set; }
    public string Name { get; set; }
    public decimal AverageOrderAmount { get; private set; }
    public DateTime LastOrderDate { get; private set; }
}

Normally, to get this data out of SQL Server, I would write a query using aggregate functions like this:

select b.BuyerID, b.Name,
avg(o.OrderTotal) as AverageOrderAmount, max(o.OrderDate) as LastOrderDate
from Buyers b
join Orders o on o.BuyerID = b.BuyerID
where BuyerID=@BuyerID
group by b.BuyerID, b.Name

My question is, how do I communicate this in my mapping? Is this possible?

I supposed I could store these calculated values in the Buyers cable and recalculate them as needed, but that doesn't feel right.

+1  A: 

From what I know, you can't map that using nhibernate. An entity represents a table(most of the times) , what you have there is more along the lines of a report.

To have access to that class I would create a view in the database and then create a separate entity. In your normal use of Buyer you probably don't always need AverageOrderAmount and LastOrderDate - I think that you're using that to display this information on the interface , in which case you should create and map a DB view.

sirrocco
Okay. That makes sense. Thanks!
sectrean