Injecting MVC with MEF 2 in 2 Minutes
- Posted: Mar 21, 2012 at 12:21 PM
- 27,704 Views
- 18 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
If you've wondered how to make sense of the alphabet soup and options available for Dependency Injection and Inversion of Control (DI/IoC) as well as how the Managed Extensibility Framework (MEF) can help you solve these problems, check out this screencast. In two minutes, you'll have a fully functioning DI/IoC-driven MVC site using MEF 2.0.
In this video, you'll see how you can get MEF running in an ASP.NET MVC site and how to successfully decouple the controller layer from the service infrastructure and allow for reuse of the service layer across multiple controllers. The Microsoft.MEF.CompositionProvider NuGet package put forth by the MEF team makes MEF an irresistible DI/IoC option for MVC developers.
Let Brady Gaster show you how to inject your MVC site using Visual Studio 11 and MEF 2.0 and quickly have more SOLID code.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
Great video! However, MEF stands for Managed Extensibility Framework, not Microsoft Extensibility Framework.
Very nice! I like the dancing cursor and MEF too!
LOL--cursor dancing to Grapes!
I've always found MEF (and DI/IOC) to be ironic.
It's useless until you need it, then it's indispensible!
@dsplaisted: typo fixed!
Love the music idea instead of someone talking over the video "blah blah blah", i think it adds a nice creative style and lets you focus on the content which speaks for itself. Good work, i really hope you make more of these!
Very cool, I had never thought about using MEF for IoC. Does anyone know how it performs under heavy load compared to Unity? I've noticed that Unity isn't the fastest container on the block...
@DmitryNYC: Thanks so much! I hope I do too!
I'm assuming this only works if you have one implementation of the interface. Is there a way to configure the instance you want or is this based only on having one implementation.
Does anyone know what this music is ?
Sorry... got it
(found it on the end = http://ccmixter.org/files/grapes/16626 )
@Curtis Gibeaut: MEF has a few mechanisms for differentiating between implementations of an interface.
The simplest is to use names - there's an example here: http://blogs.msdn.com/b/bclteam/archive/2011/11/03/overriding-part-registration-conventions-with-the-mef-attributes-nick.aspx
Beyond simple naming you can also apply and consume export metadata, e.g. see: http://devlicio.us/blogs/derik_whittaker/archive/2010/03/06/providing-metadata-to-you-mef-exports.aspx.
Hope this helps!
Nick
Wow, I like magic as much as the next guy, but can any one point me to where this is all getting wired up? I dont see any configuration in Web config, or anything in Global.asax to wire anything up. Where is all of this hidden?
thanks
Noel
@nanderto: I just found this post, about half way down is the topic "Composition provider for ASP.NET MVC" which explains that the interfaces need to be in the Parts folder.
http://blogs.msdn.com/b/bclteam/archive/2011/10/27/what-s-new-in-mef-version-2-preview-4.aspx
Brady, loving the posts! You're doing a great job.
I recall listening to a podcast a few years ago that had Glenn Block stating MEF was not trying to replace other IoC containers. I actually wrote it off as something that was cool but generally overkill for the small scale projects that I do. I think the podcast was Herding Code and do realize it was a few years ago and things change fast.
It seems even simpler than Ninject, which I've been using based on it's own simplicity, but is MEF not overkill for simple internal decoupling?
Is it just as simple for MEF to do injection patterns other than constructor injection? Specifically property setter injection?
Thanks, and keep up the good work!
@nickspiers: Having one of those "not worthy" moments, man! Thanks for the amazingly supportive comments. I'm honestly thinking of making a series out of these. You guys thing that'd be a good thing? A "how to with MEF" screen cast series?
@nickspiers: I'm pretty sure MEF supports all the features you're asking about. One of the other comments from above, by Nicholas Blumhardt, was made by one of the folks responsible for MEF 2. He would definitely have some good examples out there on using it. mef.codeplex.com has some awesome resources on using the new implementation, too. It has a fluent or attribute-based wire-up methodology.
Great job Brady!
Keep spreading the MEF love!
@bradygaster
Just used it in a project and I'm sold! Thanks!
Remove this comment
Remove this thread
close