From "MEF, WTF?!" to "MEF, FTW!"

In addition to my talk on CoffeeScript at SoCalCodeCamp I also reprised my talk on testing and debugging MEF. I didn’t record this session at UCSD but if you missed it, you can watch a recording of the “premiere” at CSU Fullerton. While I covered a lot of the same ground at UCSD, I did revise the slides and rewrote the sample code from scratch. The biggest difference is that at UCSD I introduced CompositionTests at the end of the talk, which I hope lowers the barrier of entry for beginners.

This post should be short because I’ve already written a few posts on this topic. However, if you attended the session and are here looking for additional information, here’s a list of resources:

Since I don’t have a video, here’s a picture of me standing in front of a Pizza Factory.  See, it’s more that just a lame example from a design patterns book.

WP_000324

A Few Comments on the Sample Code

As I mentioned I rewrote the code. Mostly because the old code was way too complicated. For some reason I was obsessed with using DirectoryCatalogs for everything during the first six months of my career with MEF. In the last six months I’ve come to appreciate the other catalogs, especially while refactoring and certainly for demos. This helped simplify the code a great deal.

Having the benefit of giving the talk before, I knew that I didn’t have time to cover all the failure scenarios I programmed into the original demo. I think I had about 8 different scenarios in the original PizzaStore, and in reality there is only time to demo about two. So, while rewriting the code, I only setup two scenarios, one failure that doesn’t throw an exception, and one that does.

To demonstrate the difference between .NET 4 and 4.5, I used a separate solution. The first time around I copied the solution folder, renamed it, changed references and I was rockin’ and rollin’. This worked well enough to Demo, but the truth was that only one line of code needed to change between .NET 4 and 4.5. This time around the projects share the same files using the “Add As Link” option in Visual Studio.

If you want to poke around the code, you should know that the “Program” project does almost nothing. It holds part definitions that the “Demo” project can play with. The Program class, while it does nothing useful, does show an example of my preferred way to share ExportProviders and ComposablePartCatalogs with test code.

Over in the Demo project we use the TypeCatalog instances to control whether the composition succeeds or fails. We also have an example of implementing a composition test by hand, which includes some really ugly brute force formatting necessary to ensure that the parts are listed in the same order every time. Finally, we have an example of composition test using my own CompositionTests library, which not only removes the burden of writing broiler plate code, but handles the ordering for you.