Darf Zon
Darf Zon

Reputation: 6378

MVVM: How to maintain data which all the view models will use?

Maybe the title is not so descriptive.

I'm working right with Galasoft MVVM framework, and I realized in my application I need all the time or use these data in the view models.

The data which I need is this Authentification model

I guess it is not a good idea and the best way to pass these data through navigation.

A few months ago, I was using Prism and I remember something called Container, where you can register and save your object in all the lifecycle of your application.

Another way which I was thinking is to save it in App class, but I'm not sure if this is a good idea.

By the way, I'm working on Metro UI applications.

Upvotes: 0

Views: 364

Answers (2)

Hadi Eskandari
Hadi Eskandari

Reputation: 26414

For Authentication there's already something there you can use. Check IPrincipal/IIdentity interfaces. Upon authentication these are set to Thread.CurrentThread.CurrentPrincipal and can be used later.

The Container you are referring is probably Unity which is an IoC container and GalaSoft also comes with a simple one.

Upvotes: 0

cordialgerm
cordialgerm

Reputation: 8503

Don't store the data in the App because it is part of the View. To use it you would effectively have to reference the View from all of your ViewModels. A core principle of MVVM is that the Model(s) only have access to other Model items, the ViewModel has access to other ViewModels and the Model, and the View has access to ViewModels

There are a couple of choices you could consider

  1. Create a static class AuthenticationData where this gets initialized
  2. Pass the authentication data to the constructor for each view model
  3. Store the authentication data in an IoC container (GalaSoft has a SimpleIoc class)

Options 2 and 3 could make it easier to mock your authentication data for testing purposes if you use an IAuthenticationData interface

Upvotes: 1

Related Questions