Question

The project I am working on is a mobile .NET CF based application. I have to implement the MVP pattern in it. I am now using OpenNETCF.IoC library and Services in it.

I have to refactor Windows Forms code to SmartParts.

I have a problem in implementing navigation scenario:

// Show Main menu    
bodyWorkspace.Show(mainMenuView);

// Show First view based on user choice    
bodyWorkspace.Show(firstView);

// In first view are some value(s) entered and these values should be passed to the second view    
bodyWorkspace.Show(secondView); // How?

In Windows Forms logic this is implemented with variables:

var secondForm = new SecondForm();
secondForm.MyFormParameter = myFormParameter;

How can I reimplement this logic in MVP terms?

Was it helpful?

Solution

It greatly depends on your architecture, but this would be my suggestion:

First, ViewB does not need information in ViewA. It needs information either in the Model or a Presenter. ViewA and ViewB should get their info from the same place.

This could be done, as an example, by a service. This could look like this:

class ParameterService
{
    public int MyParameter { get; set; }
}

class ViewA
{
    void Foo()
    {
        // could also be done via injection - this is just a simple example
        var svc = RootWorkItem.Services.Get<ParameterService>();
        svc.MyParameter = 42;
    }
}

class ViewB
{
    void Bar()
    {
        // could also be done via injection - this is just a simple example
        var svc = RootWorkItem.Services.Get<ParameterService>();
        theParameter = svc.MyParameter;
    }
}

Event Aggregation, also supported in the IoC framework you're using, could also work, where ViewA publishes an event that ViewB subscribes to. An example of this can be found here, but generally speaking you'll use the EventPublication and EventSubscription attributes (the former on an event in ViewA, the latter on a method in ViewB).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top