26
Fun with ASP.NET MVC 3, MEF and NuGet Pure application LEGO Maarten Balliauw AZUG / VISUG @maartenballiauw http://blog.maartenballiauw.be

Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)

Embed Size (px)

Citation preview

Page 1: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)

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)

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)

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)

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)

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)

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)

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)

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)

demo in ASP.NET MVC 3

DEPENDENCY INJECTION

Page 10: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)

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)

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)

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)

demoand finding it on NuGet

USING MEFCONTRIB

Page 14: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)

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)

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)

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)

demoapplication components

CREATING

Page 18: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)

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)

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)

demoa private NuGet feed

CREATING IT THE LAZY WAY

Page 21: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)

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)

demoCover Sheet Generator

BUILDING AN APPLICATION

Page 23: Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)

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)

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)

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)

Q&ATHANK YOU FOR JOINING

Me, having a question

http://blog.maartenballiauw.be @maartenballiauw