Building applications with WPF, MVVM and Prism(aka CAG)

Building applications with WPF, MVVM and Prism(aka CAG)

In this article I am going to walk through an application using WPF and Prism (aka composite application guidance, CAG) which simulates engaging a taxi (cab).

Building applications with WPF, MVVM and Prism(aka CAG)

  • A login screen to authenticate the user
  • An information screen.
  • A screen to engage the cab and roam around and calculating the total fare

Building applications with WPF, MVVM and Prism(aka CAG)

  • one-fifth of a mile, when the cab is traveling at https://rapidloan.net/payday-loans-ca/ 6 miles an hour or more; or
  • 60 seconds when not in motion or traveling at less than 12 miles per hour.
  • Night surcharge of $.50 after 8:00 PM before 6:00 AM
  • Peak hour Weekday Surcharge of $1.00 Monday – Friday after 4:00 PM before 8:00 PM
  • New York State Tax Surcharge of $.50 per ride.

Building applications with WPF, MVVM and Prism(aka CAG)

  • Friday (2010-10-08)

Before we dive into the app, I would like to give brief description about the framework. If you want to jump on to the source code, scroll all the way to the end of the post.

MVVM pattern is in no way related to the usage of PRISM in your application and should be considered if you are using WPF irrespective of PRISM or not.

Lets say you are not familiar with MVVM, your typical UI would involve adding some UI controls like text boxes, a button, double clicking on the button, generating event handler, calling a method from business layer and updating the user interface, it works most of the time for developing small scale applications. The problem with this approach is that there is some amount of code specific to business logic wrapped in UI specific code which is hard to unit test it, mock it and MVVM helps to solve the exact problem.

MVVM stands for Model(M) – View(V) – ViewModel(VM), based on the interactions with in the three parties it should be called VVMM, MVVM sounds more like MVC (Model-View-Controller) so the name.

WPF allows to create user interfaces using XAML and MVVM takes it to the next level by allowing complete separation of user interface and business logic.

In WPF each view will have a property, DataContext when set to an instance of a class (which happens to be your view model) provides the data the view is interested in, i.e., view interacts with view model and at the same time view model interacts with view through DataContext. Sujith, if view and view model are interacting directly with each other how does MVVM is helping me separation of concerns? Well, the catch is DataContext is of type Object, since it is of type object view doesn’t know exact type of view model allowing views and views models to be loosely coupled.

View models aggregate data from models (data access layer, services, etc) and make it available for views through properties, methods etc, i.e., View Models interact with Models.

PRISM

Prism is used to build composite applications, the framework is provided by Microsoft Patterns and Practices team and it can be downloaded from codeplex for source code, samples and documentation on msdn . The name composite implies, to compose user interface from different modules without direct dependencies on each other, again allowing loosely coupled development.

Well Sujith, I can already do that with user controls, why shall I learn another framework? That’s correct, you can decouple using user controls, but you still have to manage some amount of coupling, like how to do you communicate between the controls, how do you subscribe/unsubscribe, loading/unloading views dynamically. Prism is not a replacement for user controls, provides the following features which greatly help in designing the composite applications.