Structure of assemblies in a project using the MVVM pattern

2

I'm having difficulty organizing a project solution using the MVVM pattern. I do not use any MVVM framework.

I currently have the following structure:

Solution
    |
    --- AppView (Projeto principal onde estão as views e que é iniciado pelo App.xaml)
    |
    --- AppViewModel (Assembly que contém as viewmodels)
    |
    --- AppModel (Assembly que contém os modelos de dados - Pessoa.cs, Cliente.cs, etc...)

References are:

AppView -> AppViewModel -> AppModel

When you start the application, AppView.MainWindow is displayed. It has viewmodel - AppViewModel.MainWindowViewModel (in separate assembly).

The difficulty arose when I need this viewmodel to open another Window , since I can not do this in the AppViewModel assembly, due to the references.

So I would like to know if anyone has an example of pattern MVVM with a structure in which the assemblies of view and viewmodel are different.

    
asked by anonymous 22.02.2014 / 15:49

1 answer

1

This is done by callbacks and delegates, which is a callable function object. At some point you will have to fill this function object by instantiating your ViewModel class. I think the most correct way is by interfaces. You can do something like this:

Interface:

public interface IObjeto
{
    void MeuMetodo();
}

AppView object:

public class Objeto: IObjeto
{
    public void MeuMetodo()
    {
        // Faz alguma coisa, como chamar a janela, por exemplo.
    }
}

ViewModel class:

public class ClasseDoViewModel
{
    private IObjeto _objetoDoCallback;

    public ClasseDoViewModel(IObjeto objetoDoCallback) 
    {
        _objetoDoCallback = objetoDoCallback;
    } 

    public static void ChamarJanela(IObjeto objetoDoCallback)
    {
        objetoDoCallback.MeuMetodo();
    }
}

You can even extend this pattern to your AppModel .

    
22.02.2014 / 21:04