HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Caliburn Micro communication between list and edit viewmodels

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
microeditcommunicationbetweencaliburnviewmodelsandlist

Problem

I'm new at Caliburn Micro and the MVVM pattern.

I have 3 views (with its corresponding viewmodels):

  • AppView



  • ClientsView



  • EditClientView



AppView is my main page with a TabControl inside. The child views (WPF UserControls) are loaded in new tabs.

In ClientsView I have a grid with filtered clients. I select one, and when the Modify button is clicked I need to load a new tab with the editClientView and load its data. Here is my code:

public class EventAggregationProvider
{
    static EventAggregator _eventAggregator = null;
    public static EventAggregator EventAggregator
    {
        get
        {
            if (_eventAggregator == null)
                _eventAggregator = new EventAggregator();

            return _eventAggregator;
        }
    }
}


Function on ClientsViewModel:

public void ModificarCliente()
    {
        AppViewModel myParent = (AppViewModel)this.Parent;
        myParent.OpenTab(typeof(modClienteViewModel));
        EventAggregationProvider.EventAggregator.Publish(_selectedclient);
    }


MainViewModel OpenTab function:

public void OpenTab(Type TipoVista)
{
    bool bFound = false;
    if (TipoVista != null)
    {
        Screen myScreen = (Screen)Activator.CreateInstance(TipoVista as Type);
        myScreen.DisplayName = myScreen.ToString();
        foreach (Screen miItem in Items)
        {
            if (miItem.ToString() == myScreen.ToString())
            {
                bFound = true;
                ActivateItem(miItem);
            }
        }
        if (!bFound) ActivateItem(myScreen);
    }
}


EditClientViewModel:

```
class modClienteViewModel : Screen, IHandle
{
private OhmioService.OhmioServiceClient serviceClient =
new OhmioService.OhmioServiceClient();

public Clientes Cliente { get; set; }

public modClienteViewModel()
{
EventAggregationProvider.EventAggregator.Subscribe(this);
Cliente = new Clientes();

Solution

Naming

You should use PascalCase to name methods and types. You do this most of the time, but slip up with modClienteViewModel

Your parameters should be in camelCase. Again, you do this most of the time, but slip up with TipoVista in your OpenTab method.

Avoid Hungarian notation, with a modern IDE it's not necessary, and it just makes types harder to read.

bool bFound = false;

should be

var found = false;

The same probably goes for miItem which can just be item without any loss of meaning.

Var

Prefer to use var in a declaration when the right-hand side makes the type obvious.

e.g.

AppViewModel myParent = (AppViewModel)this.Parent;

is obvious because of the cast, so we write

var myParent = (AppViewModel)this.Parent;

The reasoning behind this is that if you wanted to change a type, you would have to do so in two places in one line. With var, it is only one change.

You should also use var when defining the loop variable in a foreach loop.

Context

StackExchange Code Review Q#48261, answer score: 2

Revisions (0)

No revisions yet.