MVVM model and semi-global data

I developed the WPF code based on MVVM and need a little refactoring, but before that I need to decide the best architecture.

First, I started with an application that could present several similar (but separate) representations of my data. Let me call him RecordsViewModelthat has the appropriate RecordsView. Over time, I entered SettingsViewModel, which was passed to the constructor RecordsViewModeland published explicitly (allowing you to RecordsViewuse it). SettingsViewModelregistered so that the changes are reflected in all my views.

Now I want to break up a little RecordsView, because now it contains two different kinds.

I have a problem:

  • new ( RecordsMainViewand RecordsAlternativeView) both want to see the settings.
  • unlike the previously created RecordsView RecordsView, these new views are created from Xaml (the default constructor).

So my options are as follows:

  • Scroll up the tree to find the parent with the settings
  • Make a settings DependencyPropertyon the controls and make Xaml connect the property to the instance.
  • Make SettingsViewModela Singleton.

Any other better options? What do you think is the best?

+5
source share
1 answer

I would turn your settings logic into a service ( ISettingsService) and use service localization or dependency injection to get this service from what it needs.

, /DI , . , , . , , , . , .

+3

All Articles