Click here to Skip to main content
15,886,806 members
Articles / Desktop Programming / WPF

MVVM # Episode 3

Rate me:
Please Sign up or sign in to vote.
4.89/5 (19 votes)
13 Dec 2013CPOL12 min read 61.4K   1K   65  
Using an extended MVVM pattern for real world LOB applications: Part 3
using Messengers;
using Service;
using ViewModels;
using Views;

namespace Controllers
{
	/// <summary>
	/// The controller 'is' the application.
	/// Everything is controlled by this :
	/// it instantiates Views and ViewModels
	/// it retrieves and stores customers via services
	/// 
	/// But it does all this only in response to requests
	/// made by the ViewModels.
	/// 
	/// e.g. a ViewModel may request a list of customers
	/// e.g. a ViewModel may want to save changes to a customer
	/// 
	/// set up as a partial class for convenience
	/// </summary>
	public partial class CustomerController : BaseController, ICustomerController
	{
		private static ICustomerService CustomerService;

		#region Constructors
		/// <summary>
		/// Private constructor - we must pass a service to the constructor
		/// </summary>
		private CustomerController()
		{
		}

		/// <summary>
		/// The controller needs a reference to the service layer to enable it to make service calls
		/// </summary>
		/// <param name="customerService"></param>
		public CustomerController(ICustomerService customerService)
		{
			CustomerService = customerService;
		}
		#endregion


		#region Public Methods
		/// <summary>
		/// Main entry point of the Controller.
		/// Called once (from App.xaml.cs) this will initialise the application
		/// </summary>
		public void Start()
		{
			ShowViewCustomerSelection();
		}
		/// <summary>
		/// Edit the customer with the Id passed
		/// </summary>
		/// <param name="customerId">Id of the customer to be edited</param>
		/// <param name="daddy">The 'parent' ViewModel who will own the ViewModel that controls the Customer Edit</param>
		public void EditCustomer(int customerId, BaseViewModel daddy = null)
		{
			BaseView view = GetCustomerEditView(customerId, daddy);
			view.ShowInWindow(true, "Edit Customer");
		}

		/// <summary>
		/// A Customer has been selected to be edited
		/// </summary>
		/// <param name="data">The CustomerListItemViewData of the selected customer</param>
		/// <param name="daddy">The parent ViewModel</param>
		public void CustomerSelectedForEdit(CustomerListItemViewData data, BaseViewModel daddy = null)
		{
			// Check in case we get a null sent to us
			if (data != null && data.CustomerId != null)
			{
				NotificationResult result = Messenger.NotifyColleagues(MessageTypes.MSG_CUSTOMER_SELECTED_FOR_EDIT, data);
				if (result == NotificationResult.MessageNotRegistered || result == NotificationResult.MessageRegisteredNotHandled)
				{
					// Nothing was out there that handled our message, so we'll do it ourselves!
					EditCustomer((int)data.CustomerId, daddy);
				}
			}
		}
		#endregion

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Devo
Australia Australia
Software developer par excellence,sometime artist, teacher, musician, husband, father and half-life 2 player (in no particular order either of preference or ability)
Started programming aged about 16 on a Commodore Pet.
Self-taught 6500 assembler - wrote Missile Command on the Pet (impressive, if I say so myself, on a text-only screen!)
Progressed to BBC Micro - wrote a number of prize-winning programs - including the best graphics application in one line of basic (it drew 6 multicoloured spheres viewed in perspective)
Trained with the MET Police as a COBOL programmer
Wrote platform game PooperPig which was top of the Ceefax Charts for a while in the UK
Did a number of software dev roles in COBOL
Progressed to Atari ST - learned 68000 assembler & write masked sprite engine.
Worked at Atari ST User magazine as Technical Editor - and was editor of Atari ST World for a while.
Moved on to IBM Mid range for work - working as team leader then project manager
Emigrated to Aus.
Learned RPG programming on the job (by having frequent coffee breaks with the wife!!)
Moved around a few RPG sites
Wrote for PC User magazine - was Shareware Magazine editor for a while.
Organised the first large-scale usage of the Internet in Australia through PC User magazine.
Moved from RPG to Delphi 1
Developed large applications in Delphi before moving on to VB .Net and C#
Became I.T. Manager - realised how boring paper pushing can be
And now I pretty much do .Net development in the daytime, while redeveloping PooperPig for the mobile market at night.

Comments and Discussions