Mobile MVC framework (part 4) - passing data between controllers

This is the part 4 of the series of the posts related to the Mobile MVC framework that I have described for you in my previous posts. I've updated the framework to handle the scenario of passing data between different controllers: the Controller class now implements two new methods:

public void Initialize(params object[] parameters)

and:

protected virtual void OnInitialize(params object[] parameters)

The NavigationService class has also been updated to support passing the parameters to its NavigateMethod:

public static void Navigate(string name, params object[] parameters)

public

static void Navigate(Controller controller, params object[] parameters)

So in order to illustrate the usage scenario I've modified the MVCDemoClient sample by adding a new DetailForm (and its DetailController). This form displays the details of the Product selected on the SearchForm which looks like this:

The code for the SearchForm:

public partial class DetailForm : ViewForm,

IView<NorthwindDataSet.ProductsRow>

{

[PublishEvent("OnBack")]

public event EventHandler OnBackEvent;

public DetailForm()

{

InitializeComponent();

}

// Callback from the Controller

private void OnProductLoaded(object sender, EventArgs e)

{

// Load the product details into controls

this.txtProductID.DataBindings.Add("Text",

this.ViewData.Model, "ProductID");

this.txtProductName.DataBindings.Add("Text",

this.ViewData.Model, "ProductName");

this.txtUnitPrice.DataBindings.Add("Text",

this.ViewData.Model, "UnitPrice");

this.txtCategoryID.DataBindings.Add("Text",

this.ViewData.Model, "CategoryID");

}

#region IView<NorthwindDataSet.ProductsRow> Members

public new ViewDataDictionary<NorthwindDataSet.ProductsRow> ViewData

{

get;

set;

}

#endregion

#region event handlers

private void menuItemBack_Click(object sender, EventArgs e)

{

if (OnBackEvent != null)

{

OnBackEvent(this, EventArgs.Empty);

}

}

#endregion

}

And now the DetailController:

public class DetailController : Controller<NorthwindDataSet.ProductsRow>

{

[PublishEvent("OnProductLoaded")]

public event EventHandler ProductLoaded;

public DetailController(IView<NorthwindDataSet.ProductsRow> view)

: base(view)

{

}

// Selected event from the view

private void OnBack(object sender, EventArgs e)

{

// Move back to the search form

NavigationService.GoBack();

}

protected override void OnInitialize(params object[] parameters)

{

this.view.ViewData.Model = parameters[0]

as NorthwindDataSet.ProductsRow;

// Notify the view

if (this.ProductLoaded != null)

{

this.ProductLoaded(this, EventArgs.Empty);

}

}

}

In the code above, we override the OnInitialize method into which the parameters from the SearchForm will passed via NagivationService class like so:

private void ShowDetailView(NorthwindDataSet.ProductsRow product)

{

DetailController controller = new DetailController(new DetailForm());

NavigationService.Navigate(controller, product);

}

This ShowDetailView method is a part of the SearchConroller which is called when a user indicates that a Product has been selected in the ListBox on the SearchForm:

// Selected event from the view

private void OnSelected(object sender,

DataEventArgs<NorthwindDataSet.ProductsRow> e)

{

this.ShowDetailView(e.Value);

}

I have also added the Northwind.sdf database as well as the generated strongly typed dataset to the modified sample. This is why you should probably notice the NorthwindDataSet.ProductRow data type which is used to pass the Product data between the controllers. As usual, you can download the sample code with the updated MVC framework.

MVCDemoClient_4.zip