Hi Juan,
I've coded all of this successfully. I have an observer class that monitors a Root Entity & all its children recursively. So, you can do calculated fields on children n-levels deep.
Here is the basic principle (minus all the complex Recursion):
- Subscribe to ChildCollection.EntityAdded & ChildCollection.EntityRemoved Events - Listens for Collection changes
- Subscribe to either all Child Entity.EntityChanged Events or ChildCollection.ListChanged - Listens for changes to Child values
- Upon a Change - Call OnPropertyChanged("MyCalculatedField") to update the Calculated field in your Root Entity. This will use INotifyPropertyChanged to update all databound controls.Hope this helps!
Ryan
PS. I've gotten this to work so smoothly... All I have to do in each Entity's partial class is implement my ICalculatedFields interface. My CommonEntityBase automatically knows which properties are calculated fields and automatically updates them using .OnPropertyChanged() whenever the entity is changed. CommonEntityBase knows how to identify a calculated property from a real DB property by analyzing the HelperClasses.EntityFields code below. Notice how these are shared fields, not properties. Reflection can tell the difference.
So, in the same file as the partial Entity class, I also have a partial class for that Entity's Fields (shown below). Notice the Constructor - Calling a method that automatically creates new EntityFields objects and assigns them to these shared fields using Reflection. This way, I never use strings to identify my calculated fields - I always use strongly-typed EntityField objects.
For example: I can databind a readonly Label to - ProductionOrderFields.PricePerPound
And I can even allow my Calculated fields to be writable, which in turn updates a real Entity value, which in turn fires .OnPropertyChanged() for any other dependent calculated fields.
Namespace NovocDataObjects.HelperClasses
Partial Public Class ProductionOrderFields
'---------------------------------------------------------------------------
'Calculated Fields
'---------------------------------------------------------------------------
Public Shared LotNumber As EntityField
Public Shared PricePerPound As EntityField
Public Shared PricePerGallon As EntityField
Public Shared TotalPrice As EntityField
Public Shared CanProduce As EntityField
Public Shared OrderQuantityLbsIncludingWaste As EntityField
Public Shared ProductionOrderStatus As EntityField
Public Shared QualityResult As EntityField
Public Shared OrderQuantityGals As EntityField
'---------------------------------------------------------------------------
'Fields on Related Entities
'---------------------------------------------------------------------------
Public Shared FinishedProductName As EntityField
Public Shared FinishedProductDescription As EntityField
Public Shared CustomerName As EntityField
Public Shared Ponumber As EntityField
Public Shared SalesOrderNumber As EntityField
Shared Sub New()
KonectCommonVWG.LLBLGenCommon.HelperClasses.AutomaticallyCreateEntityFields()
End Sub
End Class
End Namespace