WPF application variable while maintaining verifiability

I am working on a WPF application using the MVVM pattern.

Each ViewModel will need access to a security object, which essentially provides information about the rights that the user has. Since this object needs to be filled only once at startup, and since its filling is (at least potentially) expensive, I want to keep it in a state for the life of the application.

I can make this a static variable in the application that will make it available to the entire application (at least this is my understanding). This would greatly simplify the implementation of implementations in the ViewModel, since a call to App.SecurityObject would be built into each ViewModel. I would have to make sure that the application is accessible for each test and makes fun of calling App.SecurityObject (I'm not even sure if this will work, actually).

We use StructureMap, so I can create a SecurityObjectProvider and configure it with the Singleton life cycle in the container and just make it part of every ViewModel constructor. The downside would be that (as I said) the provider had to be part of every View Model constructor.

There are other, hacky workarounds that I can think of, but they will include the creation of methods (perhaps in the base View Model class) that would allow you to enter a security object after creating the instance only for testing purposes. I usually try to avoid this kind of "for testing only" code.

It seems like this will be a common problem, but I cannot find any SO questions that are completely at a point.

+3
source share
3 answers

Thread.CurrentPrincipal. , ( Principal.IsInRole ..), , , .

unit test, Thread.CurrentPrincipal SUT, , Fixture Teardown.

Thread.CurrentPrincipal , , Decorator, . , . , , , AOP - (, Decorator).

, Ambient Context. "" , "Locator" , , Local Default, , NullReferenceExceptions , .

+3

locator . - , , , , :

var securityService = ServiceLocator.Resolve<ISecurityService>();

mocks/stubs.

+2
0

Source: https://habr.com/ru/post/1733386/


All Articles