How to Troubleshoot Apps for the Modern Connected Worker
Fun with ASP.NET MVC 3, MEF and NuGet (#comdaybe)
1. Fun with ASP.NET MVC 3,MEF and NuGet Pure application LEGO Maarten Balliauw AZUG / VISUG @maartenballiauwhttp://blog.maartenballiauw.be
2. Notes (hidden) 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. So what’s this talk about? Show you how you can build an app like Lego Show you how you can create Lego blocks for your team This is NOT a “general recommended approach”, just an idea that proved to work at one of our customers.
3. 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
4. Agenda Technologies & techniques used ASP.NET MVC 3 Managed Extensibility Framework (MEF) NuGet Creating application components Building an application Conclusion Further information Q&A
5. 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
6. var partA =new PartA(new PartB()) Dependency Injection? I need a “Part B” ! Coming up! Part A Me on a typicalwork day
8. What about ASP.NET MVC 3? ASP.NET MVC 3 uses DependencyResolver : IDependencyResolver GetService() GetServices() Register it on application start
9. 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
11. Managed ExtensibilityFramework (MEF) Cool as ICE: Import, Compose, Export [Import]IRule rule; MEF catalog HomeController [Export(typeof(IRule)] Let me see... There you go! SomeRuleImpl MEF container
12. 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
13. 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
15. 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
16. Conventions based model public class MvcApplicationRegistry : PartRegistry { public MvcApplicationRegistry() { Scan(x => { x.Assembly(Assembly.GetExecutingAssembly());x.Directory(AppDomain.CurrentDomain.BaseDirectory + "in"); }); Part() .ForTypesAssignableFrom<IController>() .MakeNonShared() .ExportTypeAs<IController>() .ExportType() .Imports( // ... ); } }
17. 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
19. 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
20. 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
24. 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)
25. Further information On the Internet: www.nuget.org www.myget.org mefcontrib.codeplex.com
26.
27. Tweet or blog the picture, link to www.buildwindows.com, use tag #bldwin
28. Wear the t-shirt and be there for the closing prize drawhttp://www.buildwindows.com/
29. Thank you for joiNING http://blog.maartenballiauw.be @maartenballiauw Me, having a question
Editor's Notes
Demo01_MVC_DependencyResolverAdd a property to the HomeController for specifying the welcome messageAdd a class “SimpleDependencyResolver.cs”Implement IdependencyResolver (use snippets for some parts) public class SimpleDependencyResolver : IDependencyResolver { public object GetService(Type serviceType) { if (serviceType == typeof(Controllers.HomeController)) { var controller = Activator.CreateInstance(serviceType) as Controllers.HomeController; controller.MessageText = "Welcome, this text has been injected!"; return controller; } if (serviceType.IsInterface) { if (serviceType == typeof(IControllerFactory)) return new DefaultControllerFactory(); if (serviceType == typeof(IControllerActivator)) return null; if (serviceType == typeof(IFilterProvider)) return GlobalFilters.Filters; if (serviceType == typeof(IViewEngine)) return new RazorViewEngine(); if (serviceType == typeof(IViewPageActivator)) return null; } return Activator.CreateInstance(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { return new object[] { GetService(serviceType) }; } } Register dependencyresolver in App_Start:DependencyResolver.SetResolver(new SimpleDependencyResolver());
Demo02_MefContribCreate a new MVC application (application template)Add an IHelloWorldService public interface IHelloWorldService { string Hello(); } Add a HelloWorldService [Export(typeof(IHelloWorldService))] public class HelloWorldService : IHelloWorldService { public string Hello() { return "Hello from HelloWorldService!"; } } Change HomeController public class HomeController : Controller { private IHelloWorldService service; [ImportingConstructor] public HomeController(IHelloWorldService helloWorldService) { this.service = helloWorldService; } public ActionResult Index() { ViewBag.Message = this.service.Hello(); return View(); } public ActionResult About() { return View(); } } Run and fail…NuGet the MefContrib.MVC packageExplain the fact that it uses the /bin folder for part discoveryExplain the use of conventions
Initech.Components.Theming.DefaultThemeOpen solutionShow it’s nothing but a “plain old” MVC applicationRun the projectDemonstrate it does nothing, it’s just a template…Show the NuGet folder in Windows ExplorerOpen package.nuspec using NuGet Package ExplorerShow people around:Show the “Content” folder, this is where package contents will go. In this case, it will contain the Views, CSS and scripts.Show “lib”: it will contain assemblies (if appropriate for the package)Package.proj is the MSBUILD script that does the packagingOpen itSpecify some settings about what to copy and packageUpdate version number in nuspec fileRun nuget.exe on the nuspec fileAwesomeness!
Tonen aparte directoryTonen NuGet.ServerTonen MyGet.org
IniTech.TpsCoverSheetGeneratorCreate an empty MVC applicationAdd a library package reference to InitechThemeDefaultThemeUpdate _ViewStart.cshtml to use _InitechLayout.cshtmlAdd a HomeControllerAdd a viewRun the application to show off the templateRemove the view: we’ll generate that later onAdd library package reference (from command line?) to InitechDomainTpsReportsModify Index() action method: public ActionResult Index() { return View(new TpsReportCoverSheet()); } Add POST action [HttpPost] public ActionResult Index(TpsReportCoverSheet model) { if (ModelState.IsValid) { return View("TpsReportCoverSheet", model); } return View(model); } Add [Authorize] attributeAdd library package reference to InitechWebMvcAuthenticationDemonstrate it adds dependencies from NuGet as well as from Initech package repositoryWe need an implementation for the contracts… InitechAuthenticationMembershipDummy & InitechAuthenticationFormsShow the app: almost no code, we just combined some building blocks…