fun with asp.net mvc 3, mef and nuget

26

Upload: maarten-balliauw

Post on 10-May-2015

8.267 views

Category:

Technology


3 download

DESCRIPTION

So you have a team of developers… And a nice architecture to build on… How about making that architecture easy for everyone and getting developers up to speed quickly? Learn all about integrating the managed extensibility framework (MEF) and ASP.NET MVC with some NuGet sauce for creating loosely coupled, easy to use architectures that anyone can grasp.

TRANSCRIPT

Page 1: Fun with ASP.NET MVC 3, MEF and NuGet
Page 2: Fun with ASP.NET MVC 3, MEF and NuGet

Fun with ASP.NET MVC 3, MEF and NuGet

Maarten BalliauwTechnical ConsultantRealDolmen@maartenballiauw

OPN07

Page 3: Fun with ASP.NET MVC 3, MEF and NuGet

Who am I?

Maarten BalliauwAntwerp, Belgiumwww.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 4: Fun with ASP.NET MVC 3, MEF and NuGet

Agenda

Technologies & techniques usedASP.NET MVC 3Managed Extensibility Framework (MEF)NuGet

Creating application componentsBuilding an applicationConclusionFurther informationQ&A

Page 5: Fun with ASP.NET MVC 3, MEF and NuGet

ASP.NET MVC 3

All the new stuff:Razor view engineGlobal Action FiltersUnobtrusive Ajax & Client ValidationBetter Visual Studio tooling

And a very interesting one for doing Lego development:

Better support for Dependency Injection

Page 6: Fun with ASP.NET MVC 3, MEF and NuGet

var partA =new PartA(new PartB())

Sidenote: Dependency Injection?

Part A

I need a “Part B” !

Me, doing actual software development

Coming up!

Page 7: Fun with ASP.NET MVC 3, MEF and NuGet

Container

Sidenote: Dependency Injection?

Part A

I need a “Part B” !

Part B

Let me see...

There you go!

Page 8: Fun with ASP.NET MVC 3, MEF and NuGet

How does this fit into ASP.NET MVC 3?

ASP.NET MVC 3 uses DependencyResolver

: IDependencyResolverGetService()GetServices()

Register it on application start

Page 9: Fun with ASP.NET MVC 3, MEF and NuGet

How does this fit in ASP.NET MVC 3?

ASP.NET MVC will / can query the IDependencyResolver for

ControllersView engines & view pagesFiltersModel validatorsModel 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 providersModel bindersController activatorView page activator

Page 10: Fun with ASP.NET MVC 3, MEF and NuGet

Dependency Injection in ASP.NET MVC 3

demo

Page 11: Fun with ASP.NET MVC 3, MEF and NuGet

Managed Extensibility Framework (MEF)

Cool as ICE: Import, Compose, Export

MEF catalogHomeController

[Import]Ilogger logger;

ConsoleLogger

[Export(typeof(Ilogger)]

MEF container

Let me see...

There you go!

Page 12: Fun with ASP.NET MVC 3, MEF and NuGet

Options for MEF in ASP.NET MVC 3

Build an IDependencyResolverbased on MEF container

Use MefContribhas a built-in IDependencyResolverhas a “Convention” modelis available on NuGetmefcontrib.codeplex.com

Page 13: Fun with ASP.NET MVC 3, MEF and NuGet

A brief NuGet introduction...

Package management system for .NETSimplifies incorporating 3rd party librariesDeveloper focusedFree, open source

Use packages from the official feedPublish your own packagesCreate & use your own feed

Page 14: Fun with ASP.NET MVC 3, MEF and NuGet

Using MefContrib

And finding it. On NuGet.

demo

Page 15: Fun with ASP.NET MVC 3, MEF and NuGet

MefContrib.MVC3

Optional addition for MefContribAdds some things to your application:

AppStart code that does the wiringA CompositionDependencyResolverWill check all assemblies in /binWill export everything : IController by convention

Page 16: Fun with ASP.NET MVC 3, MEF and NuGet

Conventions in MefContrib-MVC3public class MvcApplicationRegistry : PartRegistry { public MvcApplicationRegistry() { Scan(x => { x.Assembly(Assembly.GetExecutingAssembly()); x.Directory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")); });

Part() .ForTypesAssignableFrom<IController>() .MakeNonShared() .ExportTypeAs<IController>() .ExportType() .Imports( // ... ); }}

Page 17: Fun with ASP.NET MVC 3, MEF and NuGet

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, actually thinking

Page 18: Fun with ASP.NET MVC 3, MEF and NuGet

Creating application components

Initech Default Theme

demo

Page 19: Fun with ASP.NET MVC 3, MEF and NuGet

What’s next?

Building itMSBuild (or whatever! Nuget.exe is all that matters)

Hosting itCreate a NuGet serverOr drop everything in a folder

Using itReference the feedDownload & install components neededAssemble using MEF (or another IoC)

Install-Package NuGet.Server

Page 20: Fun with ASP.NET MVC 3, MEF and NuGet

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 21: Fun with ASP.NET MVC 3, MEF and NuGet

Building an application

TPS ReportsCover Sheet Generator

demo

Page 22: Fun with ASP.NET MVC 3, MEF and NuGet

Conclusion

You can build an app like a Lego setRequires “bricks” (NuGet packages)Requires “glue” (MEF / MefContrib / other IoC)

Requires you to think in terms of componentsStructure is key!

Not a best-practice architectureJust something we toyed with on a projectProved to work (for the customer)

Page 23: Fun with ASP.NET MVC 3, MEF and NuGet

Further information

For time travelers:FRM06 – Deconstructing Orchard – Bradley Millington – Lagoon L (Tuesday)FRM13 – Scaffolding – Steve Sanderson – Breakers H (Wednesday)

Upcoming:FRM09 – NuGet In Depth – Scott Hanselman & Phil Haack –Lagoon H (now)

On the Internet:www.nuget.orgmefcontrib.codeplex.com

Page 24: Fun with ASP.NET MVC 3, MEF and NuGet

Thank you for joining & filling out the evals!

Q&A

Me, having a question

http://blog.maartenballiauw.be @maartenballiauw

Page 25: Fun with ASP.NET MVC 3, MEF and NuGet

Fun with ASP.NET MVC 3, MEF and NuGet

Maarten BalliauwTechnical ConsultantRealDolmen@maartenballiauw

OPN07

Page 26: Fun with ASP.NET MVC 3, MEF and NuGet

© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION

IN THIS PRESENTATION.