What is the best way to notify the view model ...

Oct 1, 2010 at 4:04 AM

I'm new to BXF and CSLA and I'm wondering what would be the best way to notify the view model that the user had changed a value in a field in the view. On the form there is a field that once a user enters a value a calculation must be preformed and the results displayed.

Would using a CSLA TriggerAction on the field looking for a ValueChanged trigger event be the best way ?

Can someone else suggest a better method.

Developer
Oct 2, 2010 at 11:00 PM

Hi Terry

I suppose you could setup a CSLA TriggerAction to subscribe to an event on the control and subsequently call a method on the view model to do the "calculation". That would work.

To me it sounds a bit like there is missing business logic (to do the heavy lifting for you). Think about the following alternative, for example:

  • Your business object has a field called DateOfBirth (public get and set)
    • In the View there is a TextBox / DatePicker that binds to it (via it's view model that is using your BO for its model).
  • Your business object has a field called Age (no public setter) which needs to store the age once your "calculation" (to extract it from DateOfBirth) happens.
    • In the View there is a TextBlock / Label that binds to it.

From my point of view this "calculation" is a "Business Method" and there is also a business rule that states, "When the DateOfBirth changes, recalculate and update the Age". Create a CSLA validation rule for the "DateOfBirth" property that simply returns true, but before doing so calls the "calculate" method that re-calculates and updates the "Age" field (SetProperty) accordingly.

If you did the above, binding and property changes notifications would do the magic for you. Basically as soon as the value for the textbox/datepicker is changed, then via binding you business object's "DateOfBirth" property is changed, which in turn triggers the validation rule (actually a mutator rule) which calls the "calculate" method. This method calls SetProperty for "Age" which will in turn (via property changed notifications and binding) will have the TextBlock/Label on the View automatically update and show the new value for the Age property. Ta-da!

I hope I'm not over simplifying things a bit here, or for that matter misunderstanding your question.

Jaans

PS: I guess it depends a bit on what is part of your core business logic and what is just relating to View bits and that's an architectural decision. For example, If you don't want to implement "Age" and the "Calculation" as part of your business object logic and rules (for whatever reason), then you could implement the "Age" property on the ViewModel as a dependency property. In that case you would then have the ViewModel subscribe to PropertyChanged events in order to "calculate" a new value for Age. Either way, your View should use Binding, and in this case against a property on the ViewModel and not a property on the Model via the ViewModel.