From cb9874aae6c12cc22f2cef5dff5cfcee02f4e0e3 Mon Sep 17 00:00:00 2001 From: Louis DeJardin Date: Mon, 15 Feb 2010 23:01:46 -0800 Subject: [PATCH] Changing how the UrlHelper is registered in IoC Added delegates to the MvcModule to resolve context-specific components --HG-- branch : dev --- src/Orchard/Mvc/MvcModule.cs | 36 +++++++++++++++++---- src/Orchard/Mvc/OrchardControllerFactory.cs | 2 -- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Orchard/Mvc/MvcModule.cs b/src/Orchard/Mvc/MvcModule.cs index 2c8964b06..27832f80d 100644 --- a/src/Orchard/Mvc/MvcModule.cs +++ b/src/Orchard/Mvc/MvcModule.cs @@ -1,5 +1,8 @@ using System.Linq; using System.Web; +using System.Web.Mvc; +using System.Web.Routing; +using Autofac; using Autofac.Builder; using Autofac.Integration.Web.Mvc; using Orchard.Controllers; @@ -9,11 +12,9 @@ using Orchard.Extensions; namespace Orchard.Mvc { public class MvcModule : Module { - private readonly ICompositionStrategy _compositionStrategy; private readonly IExtensionManager _extensionManager; - public MvcModule(ICompositionStrategy compositionStrategy, IExtensionManager extensionManager) { - _compositionStrategy = compositionStrategy; + public MvcModule(IExtensionManager extensionManager) { _extensionManager = extensionManager; } @@ -27,10 +28,31 @@ namespace Orchard.Mvc { }; moduleBuilder.RegisterModule(module); - moduleBuilder - .Register(ctx => HttpContext.Current == null ? (HttpContextBase)new HttpContextPlaceholder() : new HttpContextWrapper(HttpContext.Current)) - .As() - .FactoryScoped(); + moduleBuilder.Register(ctx => HttpContextBaseFactory(ctx)).As().FactoryScoped(); + moduleBuilder.Register(ctx => RequestContextFactory(ctx)).As().FactoryScoped(); + moduleBuilder.Register(ctx => UrlHelperFactory(ctx)).As().FactoryScoped(); + } + + static HttpContextBase HttpContextBaseFactory(IContext context) { + if (HttpContext.Current != null) { + return new HttpContextWrapper(HttpContext.Current); + } + + return new HttpContextPlaceholder(); + } + + static RequestContext RequestContextFactory(IContext context) { + var httpContext = context.Resolve(); + var mvcHandler = httpContext.Handler as MvcHandler; + if (mvcHandler != null) { + return mvcHandler.RequestContext; + } + + return new RequestContext(httpContext, new RouteData()); + } + + static UrlHelper UrlHelperFactory(IContext context) { + return new UrlHelper(context.Resolve(), context.Resolve()); } /// diff --git a/src/Orchard/Mvc/OrchardControllerFactory.cs b/src/Orchard/Mvc/OrchardControllerFactory.cs index 0c461925e..f6ac3fbfb 100644 --- a/src/Orchard/Mvc/OrchardControllerFactory.cs +++ b/src/Orchard/Mvc/OrchardControllerFactory.cs @@ -12,8 +12,6 @@ namespace Orchard.Mvc { // Locate the container this route is bound against var container = GetRequestContainer(routeData); - container.Build(cb => cb.Register(new UrlHelper(requestContext))); - // Determine the area name for the request, and fall back to stock orchard controllers var areaName = GetAreaName(routeData) ?? "Orchard";