Fun with ASP.NET MVC 3,MEF and NuGet
Pure application LEGO
Maarten BalliauwAZUG / VISUG
@maartenballiauwhttp://blog.maartenballiauw.be
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
Agenda
• Technologies & techniques used– ASP.NET MVC 3–Managed Extensibility Framework (MEF)– NuGet
• Creating application components• Building an application• Conclusion• Further information• Q&A
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
var partA =new PartA(new PartB())
Dependency Injection?
Part A
I need a “Part B” !
Me on a typicalwork day
Coming up!
Container
Dependency Injection?
Part A
I need a “Part B” !
Part B
Let me see...There
you go!
What about ASP.NET MVC 3?
• ASP.NET MVC 3 uses DependencyResolver
• : IDependencyResolver–GetService()–GetServices()
• Register it on application start
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
demo in ASP.NET MVC 3
DEPENDENCY INJECTION
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!
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
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
demoand finding it on NuGet
USING MEFCONTRIB
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
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( // ... ); }}
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
demoapplication components
CREATING
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
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
demoa private NuGet feed
CREATING IT THE LAZY WAY
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
demoCover Sheet Generator
BUILDING AN APPLICATION
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)
Further information
• On the Internet:–www.nuget.org–www.myget.org –mefcontrib.codeplex.com
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
Q&ATHANK YOU FOR JOINING
Me, having a question
http://blog.maartenballiauw.be @maartenballiauw