Build Composite WPF Applications
When you build complex UI applications, it is all too easy to end up with a messy, tightly coupled, interwoven mess that is difficult to develop and maintain; and impossible to test. Too avoid that, you need to employ good design patterns in your UI layer that help you keep things loosely-coupled and testable. Composite Application Guidance for WPF is a set of libraries, documentation, and sample code from Microsoft patterns & practices that helps you to build clean, maintainable applications. In this article, I’ll show you what this set of guidance provides and how to use it to build your WPF applications.
The first question you may be asking yourself is “What’s a composite application?” In simple terms, it is an application that is composed from multiple loosely-coupled parts. WPF itself is built on the concept of composition. You compose your UI out of granular UIElements which form the whole that the user sees as a Window or Page on their screen. Composite Application Guidance for WPF (or Composite WPF for short) focuses at a more abstract level, specifically on how you compose the code artifacts that contain both the UI definitions and the logic code that supports the UI presentation. Composite WPF provides the Composite Application Library (CAL), a Reference Implementation sample application, a set of QuickStart applications, and documentation of everything provided in the guidance.
“You could just put all your view logic in the code behind of the user control, but that is not a great idea from a separation of concerns perspective, and will reduce the testability and maintainability of your views.”
Although the simplest way to get started building a WPF application is to start adding Windows and UserControls to a project and fleshing everything out with a jumble of XAML and code-behind, this will quickly lead you down the path to a difficult to develop and maintain application. You will almost certainly end up with complex object and assembly reference paths interwoven through your code, and tight coupling between the logic code and the specific UI elements in your application.
If you care about productivity and maintainability, then you need to employ good UI design patterns to prevent this, and you need a way to allow different parts of the application to be developed independently, but still come together easily into a single, coherent whole at design and runtime. You will also want to be able to unit test the logic in your UI application, which requires that logic to be decoupled from the associated UI layout and controls as much as possible. This takes a little more up front work as you define each of the pieces your application is composed of, but will result in an overall reduction in your development time because it avoids the downstream complexities that really get you bogged down in integration, testing, and maintenance.
Composite WPF helps you do this by providing the following features:
- Modular loading: You can decompose your application into multiple loosely-coupled modules that get dynamically loaded at runtime.
- UI composition: You can dynamically compose portions of your UI out of loosely-coupled views, and you can follow UI design patterns to keep the logic code decoupled from the UI definition.
- Commands: You can use a commanding infrastructure that overcomes limitations in WPF Routed Commands and that allows command handlers to be decoupled from the UI itself.
- Loosely-coupled Pub/Sub events: You can publish and subscribe to strongly-typed events from logic code scattered across your application without any direct references between publishers and subscribers. You also have control over what thread the event notifications are made on, and have the ability to filter notifications based on the contents of the event data payload.
A good way to make these concepts concrete is to talk through a fairly simple application using Composite WPF. The download code for this article contains the completed sample application but I’ll spend most of the article stepping you through the core parts. This sample sits somewhere between the Stock Trader Reference Implementation sample and the QuickStarts that come with Composite WPF in terms of complexity. The business scenario is an appointment management application.
By: Brian Noyes
Composite WPF events follow the pub/sub pattern, keeping publishers and subscribers decoupled. They also support thread dispatching on the UI thread, publishers thread, or a thread pool thread. You can also specify a filter for when your subscriber is called.