Phantom updates due to decimal precision of calculated properties

This article describes my problem. I have several properties that are calculated. They are typed as decimal (9.2) in SQL Server and decimal numbers in my C # classes. Example problem:

  • Object loaded with property value 14.9
  • The calculation is performed, and the property value changes to 14.90393
  • When a session is cleared, NHibernate issues an update because the property is dirty
  • Since the database field is decimal (9.2), the stored value does not change

Basically, a phantom update is issued every time this object loads. I don’t want to truncate calculations in my business objects, because it tightly links them to the database, and I don’t want to lose accuracy in other calculations. I tried to set the scale and precision or CustomType ("Decimal (9,2)") in the mapping file, but this seems to only affect the creation of the circuit.

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

+4
source share
2 answers

Only the rounded property will open

+1
source

It seems to me that this is more of a domain problem than a NHibernate problem.

You must copy the decimal fields into the DTO object to complete all your calculations, and then pass it back to your model / repository method along with the original NH domain object. At this point, you can apply rounding to achieve your accuracy, and then compare the rounded number with the original NH and update and save as needed.

0
source

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


All Articles