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.
Comments
Anonymous
November 05, 2008
Nice post again, thanks. I'm just missing the updated MVC framework in the attached MVCDemoClient_4.zip file. And just another idea for some of your next posts - did you ever play with some IoC framework for CF + your MVC framework? I'm currently developing a .Net CF application based on first version of your MVC framework which I connected with IoC (http://code.google.com/p/compactcontainer/) and it works really nice. I was missing these two concepts (MVC + IoC) in my CF application a lot.Anonymous
November 05, 2008
There's the System.Mobile.Mvc assembly in the Include folder of the project. I am still working on getting the source code published. I agree, the IoC is also a good idea. -AlexAnonymous
April 09, 2009
Hi Alex, my name is Sebastian, i was doing some kind of Code Kata with windows mobile, and i was looking for some mvc framework to use in my project. This one looks great, i started using it here : http://code.google.com/p/timetrackermobile. i will be giving you some feedback, thanks and good jobAnonymous
April 13, 2010
Hi priozersk, I am a newbie to mobile application development. The architecture developed by you is very promising and I think it will be well fitted to my requirements. I wanted to know if u have released the source code of the assembly after this blog. If not I wanted to know if I can use/disassemble your assembly and customize the code as per my need. Thanks and regards, Chetan RanpariyaAnonymous
April 14, 2010
The latest code is available at codeplex: http://mobilemvc.codeplex.comAnonymous
May 05, 2010
Hi Priozersk, Thanks for the reply. I have downloaded the code and started using it. But I need to make the changes as per the requirements. I need to know if I am allowed to change the code or it is protected under any copyright law by microsoft or by urself. I can see copyright lines on top of some files in the project so I just wanted to clear my doubts regarding that. Thanks a ton again. Thanks and regards, Chetan Ranpariya