Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MEF: Stop Guessing, Start Testing

MEF: Stop Guessing, Start Testing

Related Post: http://ihadthisideaonce.com/2012/07/04/from-mef-wtf-to-mef-ftw/
Diagnosing MEF composition failure is tricky, especially when parts have two or more nested dependencies. In this scenario, MEF's error messages aren't very helpful. MEF was unable to satisfy one or more parts in the chain, but the error message points you to the part at the top of the chain, to really fix the problem you need to know about the bottom of the chain. If you start by looking where the error message sends you, you can end up feeling like you're on a wild goose chase because nothing appears to be wrong with that part.

Come to this session to discover tools that can ease your frustration with MEF, and get you out of diagnosis mode and back to coding. I'll show you how to combine these tools with ApprovalTests to create integration tests that provide early warning when something is wrong with your parts. Finally, we'll look at MEF 2 and the diagnostic improvements Microsoft has in store for the next version of this useful extensibility library.

Jim Counts

June 24, 2012
Tweet

More Decks by Jim Counts

Other Decks in Technology

Transcript

  1. MEF: Stop Guessing, Start Testing Jim Counts | [email protected] |

    @jamesrcounts http://ihadthisideaonce.wordpress.com
  2. Why care about Composition Tests? Diagnosing composition failure can be

    tricky at first. Exceptions and tracing misidentify root cause. Better open source tools are available. Better exceptions and tracing ship with .NET 4.5 Image Credit: mef.codeplex.com
  3. MEF 101 MEF is about putting parts together (composition) A

    motor is part of a car. MEF won’t give you incomplete parts (stable composition) To MEF, no such thing as “great car, just needs motor” If you can’t use it now, it doesn’t exist. MEF will give you parts if only optional parts are missing Sun roof is optional Cup holders-- optional
  4. MEF Failure Modes Composition Exception • Your module explodes and

    is unusable • Occurs when you ask MEF for a part it can’t provide. • No wheels, no problem—unless you ask for a car. Bad Behavior • Composition Succeeds, but the services you want aren’t available. • Occurs when the part you asked for has optional parts that can’t be satisfied. • Customer wont buy a car without cup holders—even though it still drives.
  5. Diagnostic Utility Belt Exceptions Debugger Inspection Trace Messages MEFX (MEF

    Explorer) Visual MEFX (Silverlight or WPF) Composition Diagnostics API (MEFX.Core) CompositionTests library
  6. RoboPizza5000 For all your pizza making needs Wants to import

    Pizza Recipes Wont ever be rejected May not know how to make the requested pizza Lets look at some diagnostics [Export(typeof(IPizzaOven))] public class RoboPizza5000 : IPizzaOven{ [ImportMany] public IEnumerable<IPizzaRecipe> PizzaRecipes { get; set; } public IPizza MakePizza(string recipe) { // ... } }
  7. MEF 2 Diagnostics • Improved Formatting • Actual Root Cause

    (sometimes) Better Exceptions • Container Option • Exception for any rejection • Great for tests, and internal composition Unstable Composition
  8. Satisfaction Checklist Do your parts have the [Export] attribute? Did

    you forget to mark the [ImportingConstructor]? Are your matching parts missing from the catalog? Are there too many matching parts in the catalog? Did you forget to use the interface name when exporting? Do your contract names match? Does each import metadata key exist on the export? Does the meta data type match? (1 != “1”) Do you have the right creation policy?
  9. Satisfaction Checklist Can the container satisfy all nested imports? Did

    the container reject changes during recomposition? Do your parts reference unavailable assemblies? Are there conflicts between different versions of the same assembly? Are your parts signed? Do you have the right version? Do your assemblies target a compatible CPU architecture? Are your assemblies available in your current load context? Do you have any other type load issues? Do your assemblies even work at all?
  10. Resources How to Debug and Diagnose MEF Failures, Daniel Plaisted’s

    Blog •http://blogs.msdn.com/b/dsplaisted/archive/2010/07/13/how-to-debug-and-diagnose-mef- failures.aspx Diagnosing Composition Problems, mef.codeplex.com •http://mef.codeplex.com/wikipage?title=Debugging%20and%20Diagnostics&referringTitle=Guide Stop Guessing About MEF Composition and Start Testing, ihadthisideaonce.com •http://ihadthisideaonce.wordpress.com/2012/01/31/stop-guessing-about-mef-composition-and- start-testing/ MEF Composition Tests, Redux, ihadthisideaonce.com •http://ihadthisideaonce.wordpress.com/2012/06/12/mef-composition-tests-redux/ CompositionTests •https://github.com/jamesrcounts/CompositionTests •http://nuget.org/packages/CompositionTests