1

Topic: [WPF] Two ViewModel for one View

Good afternoon! Is , some buttons and which commands depend on a role of the user, which  in application. We admit, there is a role of the Employee and the Chief. The type, elements and behavior specified  are almost identical for both these roles, but some buttons (and commands) are accessible only to the Employee, and some only to the Chief, and the part is accessible both to that and another, but with different behavior. View for both roles one. I see two methods of implementation of it: 1. For both roles there is one ViewModel, through the designer transfer a role of the authorized user and depending on it we display buttons and we change behavior of commands. This approach is used now, but it is not pleasant to that the code is covered by checks: if (IsWorker) {}... if (IsBoss) {}... And it is very easy to pass this check somewhere. 2. To create base class Employee with the general functional, and then for each role to redefine specific methods. Or not to create base class, and ViewModel the Chief to inherit from the Employee and also  methods. In this case we do not get rid of type checks if (IsWorker) just for show buttons and availability of commands (since View the general), but these checks will not be in methods. I.e. everyone ViewModel for the role becomes hardly to "purer". What variant to select? And I can generally something not so I do?

2

Re: [WPF] Two ViewModel for one View

Hello, TG, you wrote: TG> 2. To create base class Employee with the general functional, and then for each role to redefine specific methods. I would select this variant. TG> in this case we do not get rid of type checks if (IsWorker) just for show buttons and availability of commands (since View the general), but these checks will not be in methods. I.e. everyone ViewModel for the role becomes hardly to "purer". Use converters. <Button Visibility = "{Binding Path=IsWorker, Converter = {StaticResource BoolToVis}}"/> BooleanToVisibilityConverter Class... <<RSDN@Home 1.0.0 alpha 5 rev. 0>>

3

Re: [WPF] Two ViewModel for one View

Hello, TG, you wrote: TG> What variant to select? Yes basically, both variants the normal. In the first case, the missed checks should be checked by units-tests then you will not forget. The second variant too is quite good, and time about it speech the first variant ceased to arrange TG> In this case we already went we do not get rid of type checks if (IsWorker) just for show buttons and availability of commands (since View the general), but these checks will not be in methods. I.e. everyone ViewModel for the role becomes hardly to "purer". Checks can be pushed in model in CanExecute for ICommand, and not to torture View. And with tests you can cover.

4

Re: [WPF] Two ViewModel for one View

Hello, MxMsk, you wrote: MM> the Second variant too is quite good, and time about it speech the first variant ceased to arrange Aha already went. It is not pleasant that the business logic of different entities mixes up, it is difficult to trace implementation for a specific role.

5

Re: [WPF] Two ViewModel for one View

Hello, TG, you wrote: TG> Good afternoon! TG> is , some buttons and which commands depend on a role of the user, which  in application. TG> it is admissible, there is a role of the Employee and the Chief. The type, elements and behavior specified  are almost identical for both these roles, but some buttons (and commands) are accessible only to the Employee, and some only to the Chief, TG> and the part is accessible both to that and another, but with different behavior. TG> View for both roles one. I would create two View if in due course functionality differs more and more or in everyone View two and a half the button. There will be two simple View, two simple models, any checks.