Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I trigger alerts from the ViewModel?

I've asked this question of a few colleagues who I believe have good design sense. Interestingly, I've received varying responses. So which option do you think best fits the MVVM pattern and why?

Say I have a requirement to show an alert icon when $ greater than $10k.

  • Option 1: VM has property "Amount", XAML trigger and/or binding sets element visibility
  • Option 2: VM has property "ShowAlert", XAML binds visibility using a bool to visibility converter
  • Option 3: VM has property "AlertVisibility", XAML binds directly
  • Option 4: Other (please explain)

Thanks for the feedback!

like image 404
Kevin Avatar asked Oct 09 '22 17:10

Kevin


1 Answers

In your case you have definite business rule Shown WHEN > 10k, it should be encapsulated in ViewModel rather than View because View is aware on HOW data will be shown rather then WHEN and WHY.

So I would suggest exposing both Amount and ShowAlert properties and avoid exposing AlertVisibility property because it scatter such WPF View specific things like Visibility enum states (Hidden, Collapsed, ...)

If you would prefer to exposeAlertVisibility you could face following problem in the future - just imagine that newer versions of WPF got rid of the annoying Visibility enum and in this case you've to modify ViewModel to expose new Visibility infrastructure/property to keep View working fine, but this is not correct from the MVVM perspectives when a View behaviour encapsulated in ViewModel. So Keep it Simple and expose straightforward bool AlertVisible property.

EDIT: Regarding naming suggestion in the comment

Absolutely agree! I would suggest renaming AmountVisible to IsAmountSpecified or something like this.

like image 90
sll Avatar answered Oct 18 '22 07:10

sll