mercredi 11 novembre 2015

What is the best/recommended way to share a Model across ViewModels in WPF?

I have a WPF application with several ViewModels. Some of these ViewModels have also Observable collections of other ViewModels. I would like to use a single object (or list of objects) as a model and it needs to be accessed by all ViewModels in my Application. Simplest way Could be to pass a reference to every view model but I think thats not the best way. Alternatively I could declare the model as a static instance. These solutions seem a bit odd and not elegant. I was thinking maybe I could use MVVM Light and register my Model in the SimpleIoc as:

SimpleIoc.Default.Register<myModel>();

and then each model could access this instance by calling:

var model = ServiceLocator.Current.GetInstance<myModel>();

Is it a good idea or is there a better way to have access to a model from every ViewModel? ps: I do not want to use Dependency Injection. It is overkill for my small application.

Aucun commentaire:

Enregistrer un commentaire