Assembly structure in a project using the Pattern MVVM

Asked

Viewed 179 times

2

I’m having trouble organizing the Solution of a project using the MVVM. I don’t 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...)

The references are:

AppView -> AppViewModel -> AppModel

When starting the application is presented to AppView.MainWindow. This holds the viewmodel - AppViewModel.MainWindowViewModel (in separate Assembly).

The difficulty arose when I need this viewmodel open another Window, since I can’t do it on Assembly AppViewModel, due to references.

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

  • If "open window" is "navigate to window", this can be done with a navigation service implemented twice as much as @Cigano Morrison Mem suggested.

  • I recommend this guide: http://prismwindowsruntime.codeplex.com/

1 answer

1


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

Interface:

public interface IObjeto
{
    void MeuMetodo();
}

Object of Appview:

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

Class of the Viewmodel:

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 standard to your AppModel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.