
Composite Application Guidance for WPF: Prism Goes Prime Time
The WPF answer to CAB
is the Composite Application Guidance for WPF, formerly codename Prism.
NOTE:Since the
full name is too formal, too long and I can’t with a straight face refer to a
development platform as "CAG", we’ll just continue to refer to this technology herein
by its code name: Prism. Cool? :)
In this podcast we speak with
Brain Noyes of IDesign. Brian is not only an original advisor and consultant to
the Patterns and Practices group for Prism, but he is also a prolific writer, an
INETA member, Microsoft MVP, ASP Insider and Microsoft Regional Director for the Mid-Atlantic region. With a guy as
decorated as Brian, you’d think he was in the military!
What is Prism?
Prism is a set of guidance from the Patterns and Practices
group that aids in building composite WPF applications. To be clear though:
- Prism is not a new application block
- Prism is not a new framework
- Prism is not a new software factory (Prism does not feature automation)
Well now that we’ve gotten that out of the way, what is
Prism?
Prism is:
- A set of libraries - named the Composite Application
Library or CAL
- Patterns, documentation and sample code
- A reference application which mimics a real-world line-of-business
application
- A series of quick starts that demonstrate Prism’s discrete features
What is a composite application?
Large applications whose user interface layer is not
modularized end up with a monolithic code base that becomes very difficult to extend
and maintain. Without breaking apart the user interface layer maintenance is
hampered and working with large or distributed development teams proves
difficult.
Often in WPF developers try to modularize by breaking the UI
into user controls. Unfortunately, user controls end up needing intimate
knowledge of each other so the maintenance problem is displaced rather than
assuaged.
A composite application (in the UI sense) breaks the user
interface into smaller, independent pieces where testing and development proceed
in isolation.
Where Did Prism Come From?
The Patterns and Practices group did something very
different before coding began for Prism. Rather than host early looks inside
what the group had cooking under the covers, the team at Microsoft reached out
to several industry professionals asking them, "What do you want us to build?" After
digesting the feedback the team started first by building a real-world
application – using the program to drive the development of the guidance.
Further back on the timeline, many of the techniques found
in the Prism guidance came from the lessons learned and techniques that emerged
from building the first large-scale WPF application, Expression Blend. Blend
not only demonstrates what is possible in WPF, but also proves the hypothesis
that a substantial application can be built using a composable user interface.
What Are the Basic Elements of Prism?
The four natural breaking points of Prism are:
- Modularity*: A class library with single provides the lone
entry point for a feature. Modules act as logical wrappers for portions of your
application.
Prism features module enumerators for creating and locating
modules at run-time, and a module loader which instantiates modules for the application.
- Regions: In the same way that ASP.NE T master pages host
content placeholders, a Prism region is a placeholder for modules. This approach gives the application the
flexibility it needs to load in the appropriate features and associated UI
elements as needed.
- Composite Commands: Extends WPF routed commands to be loosely-coupled
from the visual tree and dispatch commands to multiple handlers.
- Loosely Coupled Events*: This new eventing mechanism features:
- The definition of
the publisher and subscriber to exist at any layer of the application
- Type and lifetime decoupling between
the publisher and subscriber, but with a strongly-typed communication model
- One line of code on the sender and receiver side
- Dispatch threads on the UI thread, publisher’s thread or a thread from the
pool
- Provide filter criteria using lambdas or delegate methods to decide whether
or not to notify the subscriber when the publisher’s event fires
* The modularity and eventing mechanisms are independent of
WPF and may be used to support other UI technologies.
What About Performance?
Any layer of abstraction will affect performance. Load
testing revealed the performance impact is realized upon instantiation of the forms.
Using dependency injection (which relies heavily on reflection) increased the
load time a measurable amount. When testing a form with hundreds of controls
the test reported a 15% to 20% in load time. While this latency is not trivial,
there are ways to improve the user experience in light of the extra load time.
The built-in modular loading supports deferred and on-demand loading.
NOTE: Remember the
20% increase was on a form with 100's of controls. If you are building screens
with over a hundred controls on them, perhaps the load time is the least of
your worries?
How Does Prism Serve the Developer/Designer Workflow?
The patterns found in the Prism guidance truly separate the
natural layers of the application by even aiming to have zero code in the
code-behind! The presentation layer is decoupled for testing, but has the added
benefit of being able to easily hand to a designer.
How Steep is the Learning Curve?
Some developers are facing an uphill battle when approaching
Prism. If you are new to test driven development, the principals of
separation-of-concerns, dependency injection and any other cool-kid buzzword
that radiates from the ALT.NET movement, then Prism may seem like a huge
undertaking.
If you do have exposure to the ALT.NET culture then you are
most likely beginning to fold these practices into your development regimen
already.
Resources
