hen 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.
To 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
(Composite WPF for short) is a set of libraries, documentation, and sample code from Microsoft patterns & practices that helps you to build clean, maintainable applications. This article shows you what the Composite WPF 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, a composite application is composed of multiple loosely-coupled parts. WPF itself is built on the concept of composition. You compose your UI out of granular UIElements, forming a whole that users see as a Window or Page on their screens. Composite WPF is more abstract, focusing 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 for everything provided in the guidance.
Although the simplest way to get started building a WPF application is to start adding Windows and user controls to a project and fleshing everything out with a jumble of XAML and code behind, this quickly leads 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 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.|
Composite WPF helps you do accomplish these goals 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 work through a fairly simple Composite WPF application. The downloadable code
for this article contains the completed sample application but I'll spend most of the article stepping you through the core parts. In terms of complexity, this sample sits somewhere between the Stock Trader Reference Implementation sample and the QuickStarts that come with Composite WPF. The business scenario is an appointment management application.
|Editor's Note: This article was first published in the November/December 2008 issue of CoDe Magazine, and is reprinted here by permission.|