0

We have a module with its Views, ViewModels and Models and a requirement came in to have an exact copy of the module, but part of the functionality has to be read-only. The read-only mode should be turned on by a configuration parameter. The Model part is the same - it will use the same data sources.

I see two possible solutions to this problem:

Change the existing Views and ViewModels, by manipulating controls' interactivity based on the param. Downsides of this approach is additional conditional logic. We're worried that if we do this, another requirement might come in, where we have to add more conditional logic. Upside - no duplication.

Another way is to split the existing view's into multiple components and assemble the regular module version and the read-only version using those components. We could disable interactivity on the View level in read-only versions and inherit from the ViewModel of the View which we're splitting from and override needed behaviour. The upside would be modularity. Downside - duplication, multiple places to maintain when new components are added in the future.

What's the preferred approach to solve problems like this?

Reed
  • 1,161
  • 13
  • 25

2 Answers2

3

This is a little simpler than you think.

The most elegant approach is to have a flag or value contained within the viewmodel which represents the editable state of the data (this could simply be a true/false flag, but I've also seen cases where it can be represented as an enum because there are multiple conditions or states).

Then within the view you can bind the control's IsEnabled or IsReadOnly properties to that value (using a converter if necessary to change the value to a boolean).

slugster
  • 49,403
  • 14
  • 95
  • 145
2

Obviously impossible to give a definitive answer without knowing all the specifics, but I generally take the view that if something is only required once, or maybe even twice, then I'll take the quickest and easiest route. In your case that would be to add conditional logic to the existing classes.

At the third change, I'll review and see if it needs to be split out into a separate entity. but more often than not the third (or even second) change never comes. Don't code for the future, YAGNI.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103