NHibernate.Linq - Custom / Calculated Expression

How a domain object can include a property that calculates a value from other associated database properties, so that the computed property can be used both in an instance of a domain object and in the db level by nhibernate.linq.

I would like to be able to use the property when working directly with the object:

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; 
+4
source share
1 answer

You need to duplicate the logic in class and collation. Here is an example:

Grade:

 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 the Invoice.Balance request

+3
source

Source: https://habr.com/ru/post/1300750/


All Articles