This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, What is the best way to implement a persisted calculated property? For example, if one had qty and price, and one wanted to persist the "total"? Is there some way of providing a formula (qty * price) via an attribute to the "total" property? Thanks, John |
|
|
Hi John, I'm assuming you're performing all calculations in the client -- it's not a computed column in a view or anything like that, right? If so, there are a few ways you could do this. In all cases, you will want to implement a private field for the "total" but expose only a getter property (because it makes no sense application code to set the calculated property). (If you're using the designer, set the "total" property's Generation option to FieldOnly.) Having done that, you can either: 1. Have your property getter return qty * price, ignoring the private field completely. Then override OnSaving to recalculate the total and set the private field. (LightSpeed calls OnSaving just before it saves an entity.) Note that if you're data binding to Total, you'll need to customise the Quantity and Price setters to raise property change notifications for Total, which is a bit of a pain. 2. Have your property getter return the backing field, and customise the Quantity and Price setters so that, in addition to updating the _quantity and _price backing fields, they also update the _total backing field. Again, if you're using the designer, you'll need to set Generation to FieldOnly on Quantity and Price so that you can write the custom setter. (I don't much like this solution because it's invasive and brittle, but I mention it for completeness.) 3. Have your property getter return the backing field, and override Entity.OnPropertyChanged to recalculate the backing field. This allows you to centralise the business rules for computing fields and call them from OnPropertyChanged. This can also take care of raising change notifications for Total if required, without needing you to muck around in the Quantity or Price setters. Don't forget to call base.OnPropertyChanged. My recommendation would probably be Option 3. |
|
|
Many thanks Ivan. Option 3 sounds like the answer - a little more complex than expected but will give it a try. I searched the forum and was suprised this question wasn't asked many times before. Am I going about this the wrong way? Is such a total normally calculated on the server as a computed column, or some other way? |
|
|
Hi jbadda, I'n a newbe to Mindscape, could you, please, attach a smaple regarding the OnPropertyChanged overriding? Thanks in advance CreF |
|
|
In a partial class for your entity add a override implementation for OnPropertyChanged e.g.
|
|