1

I have an application with a login screen, fairly typical scenario I guess. After the user gets authenticated, I need to store him somehow because I will then have to act based on his permissions etc. Of course there will be more properties to store as well..

What is considered the best way of achieving this? I'm currently evaluating two options:

1.) use a singleton (or a static property) and reference that in my viewmodels

2.) use App.Current.Properties and store it there. Then maybe use some static helper method to retrieve the user more easily and consistently throughout the app

Or maybe is there any other option to consider? Thank you for suggestions.

4

1 回答 1

2

Use the MEF and [Import] the authenticated user interface into your view models. This way you have a global resource available to your objects and the framework rather than your code is responsible for managing the resource. This introduces less coupling than either of your suggested solutions, which among other advantages will help with unit testing.

The Prism framework for Silverlight is based on MEF and should be adaptable to WPF.

MEF is dependency injection. You give MEF responsibility for resolving dependencies by mapping required interfaces to instances of concrete classes. You create an IAuthenticatedUser interface with methods that set and retrieve the identity. You create a model class that implements the interface and exports it to MEF. Your login ViewModel and any other ViewModel that requires the identity import the interface. MEF wires up the view models to an instance of the implementation class when it creates them. Your ViewModels use the interface as required.

于 2013-05-01T17:21:48.973 回答