2
\$\begingroup\$

I have a MvvM application that is working very fine. However I am not sure if I'm writing a clean code, especially the part handling child windows with their controller.

So to begin, i have this structure that helps me delegate methods to the controllers to open child windows when needed :

public struct ShowActions
{
    public readonly Action Show;
    public readonly Action ShowDialog;

    public ShowActions(Action show, Action showDialog)
    {
        this.Show = show;
        this.ShowDialog = showDialog;
    }
}

In the MainController class, I have a WindowServiceProvider property that is just a dictionary linking a type of Controller to a ShowActions structure :

public Dictionary<Type, ShowActions> WindowServiceProvider { get; set; }

The main window intializes the child windows like this :

public MainWindow()
    {
        InitializeComponent();
        var mainController = new MainController();
        this.DataContext = mainController;

        var authenticationController = new AuthenticationController(mainController);
        var modelController = new ModelController(mainController);

        AuthenticationWindow authenticationWindow = null;
        NewModelWindow modelWindow = null;

        mainController.WindowServiceProvider.Add(typeof(AuthenticationController), new ShowActions(null, () =>
        {
            authenticationWindow = new AuthenticationWindow(authenticationController)
            {
                WindowStartupLocation = WindowStartupLocation.CenterOwner,
                Owner = this
            };
            authenticationController.Password = () => authenticationWindow.PasswordBox.Password;
            authenticationWindow.ShowDialog();
        }));

        mainController.WindowServiceProvider.Add(typeof(ModelController), new ShowActions(null, () =>
        {
            modelWindow = new NewModelWindow(modelController)
            {
                WindowStartupLocation = WindowStartupLocation.CenterOwner,
                Owner = this
            };
            modelWindow.ShowDialog();
        }));
    }

Is this a good way to code ? I fill like it may be a bit heavy for what it's doing. And also, is it bad to have many controllers when one can handle everything ?

\$\endgroup\$
  • \$\begingroup\$ I have a doubt: why do you talk about controllers in a MVVM application? I'd say that in your code they're the VM part (at least because you assign a controller to the DataContext) but...I feel weird about it. Also: why this code is in the View part instead of the VM part? \$\endgroup\$ – Adriano Repetti Mar 13 '18 at 11:23
  • \$\begingroup\$ Bad habit, I call the ViewModel Controller but there are just ViewModels. Since the ViewModels are not allowed to manipulate the properties of the view I do the initialization of the child views and their viewModels in the View. But I also do feel like there is something wrong with this way of doing. : / \$\endgroup\$ – Mohammed Larabi Mar 13 '18 at 13:26
  • \$\begingroup\$ View models are (and should!) able change the properties in the view. For example: AuthenticationController.Password should be simply binded with the AuthenticationWindow.PasswordBox.Password thing. Also you should not expose services (which are just actions...) built in the view but they should be called from the relevant methods in the view model (methods which are...commands). Binding with commands is in-built, if you have actions in a dictionary then you need to write more and more code in your views to call them and, well, that's not how MVVM works. \$\endgroup\$ – Adriano Repetti Mar 13 '18 at 14:11
  • \$\begingroup\$ Using WPF like WinForms (or mixing with a MVC paradigm) is possible but your code will finally be MUCH more complicate than it should/may be. In short: in this small snippet there are things to review but the drawbacks of this approach are more clear when you include surrounding code. You find yourself writing more and more code to bind things together (and you get all the disadvantages of MVVM without any of its huge advantages) \$\endgroup\$ – Adriano Repetti Mar 13 '18 at 14:12
  • \$\begingroup\$ @AdrianoRepetti How can I bind Action between them ? Since Password() is a method for security reasons. But I'm starting to understand that I'm mixing MVC concepts with MvvM and I'll probably just reread the documentation, thanks for the lead. Then how can I make a ViewModel call for a child window ? \$\endgroup\$ – Mohammed Larabi Mar 13 '18 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.