![Page 1: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/1.jpg)
Fun with ASP.NET MVC 3,MEF and NuGet
Pure application LEGO
Maarten BalliauwAZUG / VISUG
@maartenballiauwhttp://blog.maartenballiauw.be
![Page 2: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/2.jpg)
Who am I?
• Maarten Balliauw• Antwerp, Belgium• www.realdolmen.com • Focus on web– ASP.NET, ASP.NET MVC, PHP, Azure, …–MVP ASP.NET
• http://blog.maartenballiauw.be • @maartenballiauw
Me, looking intelligent with glasses
![Page 3: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/3.jpg)
Agenda
• Technologies & techniques used– ASP.NET MVC 3–Managed Extensibility Framework (MEF)– NuGet
• Creating application components• Building an application• Conclusion• Further information• Q&A
![Page 4: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/4.jpg)
ASP.NET MVC 3
• All the new stuff:– Razor view engine– Global Action Filters– Unobtrusive Ajax & Client Validation– Better Visual Studio tooling
• And a very interesting one for doing LEGO development:– Better support for Dependency Injection
![Page 5: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/5.jpg)
var partA =new PartA(new PartB())
Dependency Injection?
Part A
I need a “Part B” !
Me on a typicalwork day
Coming up!
![Page 6: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/6.jpg)
Container
Dependency Injection?
Part A
I need a “Part B” !
Part B
Let me see...There
you go!
![Page 7: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/7.jpg)
What about ASP.NET MVC 3?
• ASP.NET MVC 3 uses DependencyResolver
• : IDependencyResolver–GetService()–GetServices()
• Register it on application start
![Page 8: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/8.jpg)
What about ASP.NET MVC 3?
• ASP.NET MVC will / can query the IDependencyResolver for– Controllers– View engines & view pages– Filters– Model validators– Model metadata
• Check Brad Wilson’s blog for examples on all of these– http://bradwilson.typepad.com/blog/2010/07/service-
location-pt1-introduction.html
– Value providers– Model binders– Controller activator– View page activator
![Page 9: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/9.jpg)
demo in ASP.NET MVC 3
DEPENDENCY INJECTION
![Page 10: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/10.jpg)
Managed ExtensibilityFramework (MEF)
• Cool as ICE: Import, Compose, Export
MEF catalogHomeController
[Import]IRule rule;
SomeRuleImpl
[Export(typeof(IRule)]
MEF container
Let me see...There
you go!
![Page 11: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/11.jpg)
MEF in ASP.NET MVC 3
• Build an IDependencyResolver–based on MEF container
• Use –has a built-in IDependencyResolver–has a “Convention” model– is available on NuGet–mefcontrib.codeplex.com
![Page 12: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/12.jpg)
A brief NuGet introduction...
• Package management system for .NET• Simplifies incorporating 3rd party libraries• Developer focused• Free, open source
• Use packages from the official feed• Publish your own packages• Create & use your own feed
![Page 13: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/13.jpg)
demoand finding it on NuGet
USING MEFCONTRIB
![Page 14: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/14.jpg)
MefContrib.MVC3
• Optional addition for • Adds some things to your application:–AppStart code that does the wiring–A CompositionDependencyResolver–Will check all assemblies in /bin–Will export everything : IController by
convention
![Page 15: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/15.jpg)
Conventions based modelpublic class MvcApplicationRegistry : PartRegistry { public MvcApplicationRegistry() { Scan(x => { x.Assembly(Assembly.GetExecutingAssembly()); x.Directory(AppDomain.CurrentDomain.BaseDirectory + "\bin"); });
Part() .ForTypesAssignableFrom<IController>() .MakeNonShared() .ExportTypeAs<IController>() .ExportType() .Imports( // ... ); }}
![Page 16: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/16.jpg)
This all makes me think...
• Package company components using NuGet?• Distribute them in a custom feed?• Use ASP.NET MVC 3?• Wire everything with MEF & MefContrib?• Pure application Lego!
Me, thinking
![Page 17: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/17.jpg)
demoapplication components
CREATING
![Page 18: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/18.jpg)
What’s next?
• Building it– MSBuild (or whatever! Nuget.exe is all that matters)
• Hosting it– Create a NuGet server– Drop everything in a folder– Use a NaaS solution: www.myget.org
• Using it– Reference the feed– Download & install components needed– Assemble using MEF (or another IoC)
Install-Package NuGet.Server
![Page 19: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/19.jpg)
A quick commercial plug
• Create your own NuGet feed• Packages from official feed• Uploaded/pushed packages• No need to setup & maintain your own
NuGet Server• Free! www.myget.org
![Page 20: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/20.jpg)
demoa private NuGet feed
CREATING IT THE LAZY WAY
![Page 21: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/21.jpg)
Let’s see if we can build this...TPS Reports Cover Sheet Generator (ASP.NET
MVC 3)
Domain layer
Domain.TpsReports
Authentication
AccountController
Contracts
And their implementations...
Theme
Default theme
Packagedas .nupkg
Wiredwith MEF
![Page 22: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/22.jpg)
demoCover Sheet Generator
BUILDING AN APPLICATION
![Page 23: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/23.jpg)
Conclusion
• You can build an app like a Lego set– Requires “bricks” (NuGet packages)– Requires “glue” (MEF / MefContrib / IoC)
• Requires you to think in terms of components– Structure is key!
• Not a best-practice architecture– Just something we toyed with on a project– Proved to work (for the customer)
![Page 24: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/24.jpg)
Further information
• On the Internet:–www.nuget.org–www.myget.org –mefcontrib.codeplex.com
![Page 25: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/25.jpg)
http://www.buildwindows.com/
Register today and save $500 before August 1st
Save the date: watch live stream, join local events by MSDN Belux
Win a ticket today at Communiday Day!• Take a picture wearing the BUILD t-shirt.• Tweet or blog the picture, link to
www.buildwindows.com, use tag #bldwin• Wear the t-shirt and be there for the closing prize
draw
![Page 26: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)](https://reader036.vdocuments.us/reader036/viewer/2022081519/554f8e05b4c905d25b8b503f/html5/thumbnails/26.jpg)
Q&ATHANK YOU FOR JOINING
Me, having a question
http://blog.maartenballiauw.be @maartenballiauw