Introduction
PRISM, or Composite Application Guidance/Library for WPF/Silverlight, is a library and a corresponding guidance document that helps you to build loosely coupled “composite” WPF and Silverlight applications.
The library itself “sits on top” of an Inversion of Control Container (Unity by default), and provides facilities for modularising your application, composing your UI from components, loosely coupling events and, in Silverlight’s case, only downloading parts of your application as and when they’re required.
Sounds great doesn’t it?
So, There Must Be Something Wrong With It.. Right?
Plenty has been written about PRISM, its benefits, and how to get started; but very little on why you wouldn’t use it on your next WPF/Silverlight project. I’ve put together some concerns, based both on criticisms I’ve read, and supposition on my part; and some commentary about whether I believe those concerns are well founded or not.
“I’m Only Doing a Small Project!”
Granted, PRISM is rather overkill for “toy” applications; but are you sure your application is always going to stay that small and simple? One of the big advantages of the modular nature of a PRISM app is being able to add and test new features with relative ease. I’m sure we’ve all started projects that started off as a simple little toy app, but have since grown organically into something rather more complex – possibly ending up with some pretty kludgey code at the end of it.
“It’s Complicated!”
No; it’s not. Really. There’s nothing particularly complex in PRISM, either conceptually or in the code you have to write. Once you understand the structure of a PRISM project, you’ll probably find that your code is actually easier to follow, as it is separated into clearly defined parts.
If you want an easy to follow tutorial that takes you from File.. New, through to a working application, then take a look at the excellent screencasts from Blaine Wastell’s blog.
“It Takes Too Long To Use! There’s Too Much Overhead!”
If you’ve watched some of the initial screencasts about PRISM, or had a peak at some sample code, you might be thinking that the overhead of creating all these “extra” classes and projects is just too much. While it certainly is more work than just File.. New and throwing controls on a page; once your initial project structure and bootstrapper is setup, adding new modules is no more time consuming than adding a normal User Control.
Separating your view from it’s logic; whether it be with ViewModels, the Presentation Model, or any other pattern, is going to be slightly more work; but the testability and data binding benefits far outweigh any slight increase in typing!
“It Will Add Bloat to My Silverlight App!”
The PRISM binaries, including Unity, weigh in at around 270k; and squeeze down to approximately 100k when zipped. Not exactly tiny, but not exactly Bloaty McBloater. In fact, if you take advantage of PRISM’s ability to bundle parts of your Silverlight application into different XAP files, and only load them when needed, you may find your initial payload is actually smaller than it would be without PRISM.
“You Have To Use Unity! I Don’t Want That Dependency!”
I’ve seen this repeated several times, but it’s simply not true. Although PRISM “out of the box” uses Unity, and comes with a Unity Bootstrapper; you can plug in your own IoC container without too much trouble. At the time of writing I believe there are alternatives for Spring.Net, Castle Windsor and StructureMap already written for you.
“I Want Control Over *ALL* My Damn Code!”
Firstly, if you’re writing a .Net application then chances are you are calling at least one library that you didn’t write 🙂 Secondly, PRISM is very “lightweight”. It’s a helping hand at your disposal, rather than a monolithic framework that takes over your whole application and forces you to work in a certain way. Thirdly, you can just pick and choose which parts of PRISM you want to use, it’s not an “all or nothing” proposition. Don’t like the view composition? Then don’t use it! And finally, it’s all open source, so if you don’t like the way something works – change it! 🙂
“It’s From MS – So It Sucks!”
Unfortunately if someone holds this view, then there’s probably nothing anyone can say to change their mind. While everything that comes out of Microsoft isn’t perfect, far from it, it most certainly doesn’t all suck 🙂
With PRISM, the exceedingly smart guys from the Patterns and Practices team have created an excellent set of tools and guidance to help solve some very common problems; without producing a heavy, complicated framework in the process (CAB anyone? 🙂 )
“I Can’t Frikkin’ Find It!”
Ok, I admit it, you’ve got me on this one! 🙂 Is it on MSDN? Is it on CodePlex? Both?! Is it called PRISM? The Composite Application Library (CAL)? Or maybe Composite Application Guidance for WPF and Silverlight (CAG)?
Now CAL obviously refers to the library, CAG to the guidance, and I believe PRISM is an umbrella term for the whole project, but the naming is certainly confusing, and searching for PRISM takes you to CodePlex then MSDN and back again, so it’s very easy to get confused! In fact, finding, downloading and building the library is actually MORE confusing than using the damn thing! There is actually a whole article on Sparking Client on How to Find, Download and Build Prism for Silverlight, which speaks volumes 🙂
I really don’t see what’s wrong with calling the whole thing PRISM and leaving it at that. If I say “.Net Framework”, then most developers would know what I mean. There’s certainly no need for me to call it the “Microsoft Framework for Building Managed Applications Targeting the Windows Platform (MFBMATWP)” 😉
Tell Me More!
If you want to know more about PRISM, or if you want some walkthroughs and examples of how to use it, then here’s some links that should float your boat:
- The MSDN section covers the goals and architecture of PRISM, along with some hands on examples and labs.
- Shawn Wildermuth’s recent article from MSDN Magazine, entitled Composite Web Apps With Prism, contains a good intro to Prism, and a walkthrough on putting together a Composite Silverlight app.
- Blaine Wastell has plenty of PRISM resources on his blog, including links to the excellent “getting started” screencasts from Channel 9.
- Ward Bell did an excellent video introduction to PRISM at Tech-Ed and it’s available from IdeaBlade.
- Brian Noyes did some DNRTV episodes showing PRISM basics, and PRISM Events and Commands
- There’s some good introductory information on the Sparkling Client blog, including how to download and install it 🙂
- There are some interesting additions to PRISM on the contrib Codeplex site.
- Ward Bell has an interesting article on his blog, in a similar vein to this one, where he asks Are We Overselling Prism?
- There’s a list of 10 Things To Know About Silverlight Prism on the Sparkling Client blog.
Great Post! I especially like the “It’s Complicated” bit. For whatever reason, it’s hard to get into PRISM. Once you do, it doesn’t seem very complicated, but it can take a while to get there.
Also, to let you know, I just posted a “10 Things to Know About Silverlight Prism” article that contains links to all of our videos and pocast interviews on PRISM: http://www.sparklingclient.com/prism-silverlight/
Thanks!
Thanks for the comments Erik, it certainly looks more scary than it actually is. It’s like a huge Pitbull that’s scary when you first see it, but as you get closer it rolls on it’s back to get it’s belly scratched 🙂
great article, I have done great research on MVVM pattern and have decided to use PRISM for my Silverlight app (although small, it will no doubt grow in size)
Its very easy to use, i followed a screen cast on MS Channel 9. I especially like the EventAggrigator and Commanding pattern that they provide for us.
One thing that can throw some people off is constructor injections. If your object throws a runtime exception during construction (for instance you forgot to initialize a List) it won’t be actually caught by PRISM and you’ll get a “construction failed” error instead of Null reference exception error or etc. Took me a while to catch it.
Very good
Igor
Thanks for the comment, good tip about constructor errors – hopefully this is something your unit test would pick up 🙂
Not sure why we need modules. Whats wrong with Unity being able to manage loading of the circular dependencies among services? I wrote my own – and it can handle this. Its simple enough – lock around the service being resitered – place into container for duration of lock – create dependencies inside that lock (hey they can see me cos its inside my lock) – voila – circulars all done. close lock. So why do we need modules? Unfrotunately – that’s why i wrote my own – prism is a massive lump of rot on top of what could have been simple and elegant. you never needed modules – they are what the rest of us call assemblies.
Incomplete!
At one level, yes, it’s a question of why PRISM, but in the real world, it’s “Why use PRISM instead of MEF”
When this post was written MEF wasn’t finished, .NET 4 wasn’t out and Silverlight 4 wasn’t even at Beta 1 stage.
I do apologise if my posts don’t include comparisons to technologies that didn’t exist at the time. I shall endeavour to invent a time machine to prevent this from happening in subsequent posts.