views:

79

answers:

2

This article describes my problem. I have several properties that are calculated. These are typed as decimal(9,2) in SQL Server and decimal in my C# classes. An example of the problem is:

  1. Object is loaded with a property value of 14.9
  2. A calculation is performed and the property value is changed to 14.90393
  3. When the session is flushed, NHibernate issues an update because the property is dirty
  4. Since the database field is decimal (9,2) the stored value doesn't change

Basically a phantom update is issued every time this object is loaded. I don't want to truncate the calculations in my business objects because that tightly couples them to the database and I don't want to lose the precision in other calculations. I tried setting scale and precision or CustomType("Decimal(9,2)") in the mapping file but this appears to only affect schema generation.

My only reasonable option appears to be creating an IUserType implementation to handle this. Is there a better solution?

+1  A: 

Map to a rounded property instead

James L
A: 

This seems to me to be more of a domain issue than an NHibernate issue.

You should copy your decimal field(s) to a DTO object to do all of your calculations and then pass back into your model/repository method along with the original NH domain entity. At this point you can apply rounding to meet your precision scale and then compare the rounded number to the original NH entity and update and save as necessary.

Chris Marisic