NHibernate.Linq - Custom/Calculated property expression
How can a domain object include a property that calculates a value from other database mapped properties so that the calculated property can be used in both the domain object inst开发者_运维技巧ance and the db level by nhibernate.linq.
I would like to be able to use the property when working with the object directly:
Console.WriteLine(Entity.Calculated.ToString());
And when working with nhibernate.linq
var q = from e in session.Linq<Entity>()
where e.Calculated > 0
select e;
You need to duplicate the logic in the class and the mapping. Here's an example:
Class:
public class Invoice
{
public virtual int Id { get; protected set; }
public virtual decimal Amount { get; set; }
public virtual decimal Paid { get; set; }
public virtual decimal Balance
{
get { return Amount - Paid; }
}
}
Mapping:
<class name="Invoice">
<id name="Id">
<generator class="hilo"/>
</id>
<property name="Amount"/>
<property name="Paid"/>
<property name="Balance" formula="Amount - Paid" access="readonly"/>
</class>
Now you can use Linq and query on Invoice.Balance
精彩评论