From 1644434ea7b2e168c229efbb0e960756d1a3444b Mon Sep 17 00:00:00 2001 From: Stephan Date: Thu, 25 Oct 2018 17:26:23 +0200 Subject: [PATCH] Cleanup controller factories --- .../Mvc/ContainerControllerFactory.cs | 9 ++++----- .../Mvc/ControllerFactoryExtensions.cs | 13 +++---------- src/Umbraco.Web/Mvc/MasterControllerFactory.cs | 15 +++++++-------- src/Umbraco.Web/Mvc/RenderControllerFactory.cs | 18 +++++++----------- .../Mvc/UmbracoControllerFactory.cs | 12 ++++-------- 5 files changed, 25 insertions(+), 42 deletions(-) diff --git a/src/Umbraco.Web/Mvc/ContainerControllerFactory.cs b/src/Umbraco.Web/Mvc/ContainerControllerFactory.cs index d37054fda1..74ac491df6 100644 --- a/src/Umbraco.Web/Mvc/ContainerControllerFactory.cs +++ b/src/Umbraco.Web/Mvc/ContainerControllerFactory.cs @@ -1,28 +1,27 @@ using System; using System.Web.Mvc; using System.Web.Routing; -using System.Web.SessionState; using Umbraco.Core.Composing; namespace Umbraco.Web.Mvc { public class ContainerControllerFactory : DefaultControllerFactory { - private readonly IContainer container; + private readonly IContainer _container; public ContainerControllerFactory(IContainer container) { - this.container = container; + _container = container; } protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { - return (IController)container.GetInstance(controllerType); + return (IController) _container.GetInstance(controllerType); } public override void ReleaseController(IController controller) { - container.Release(controller); + _container.Release(controller); } } } diff --git a/src/Umbraco.Web/Mvc/ControllerFactoryExtensions.cs b/src/Umbraco.Web/Mvc/ControllerFactoryExtensions.cs index 87f7227b82..07a3f2a5e3 100644 --- a/src/Umbraco.Web/Mvc/ControllerFactoryExtensions.cs +++ b/src/Umbraco.Web/Mvc/ControllerFactoryExtensions.cs @@ -14,25 +14,18 @@ namespace Umbraco.Web.Mvc /// /// /// - /// This is related to issue: http://issues.umbraco.org/issue/U4-1726. We already have a method called GetControllerTypeInternal on our MasterControlelrFactory, + /// This is related to issue: http://issues.umbraco.org/issue/U4-1726. We already have a method called GetControllerTypeInternal on our MasterControllerFactory, /// however, we cannot always guarantee that the usage of this will be a MasterControllerFactory like during unit tests. So we needed to create /// this extension method to do the checks instead. /// internal static Type GetControllerTypeInternal(this IControllerFactory factory, RequestContext requestContext, string controllerName) { - var controllerFactory = factory as MasterControllerFactory; - if (controllerFactory != null) - { + if (factory is MasterControllerFactory controllerFactory) return controllerFactory.GetControllerTypeInternal(requestContext, controllerName); - } //we have no choice but to instantiate the controller var instance = factory.CreateController(requestContext, controllerName); - if (instance != null) - { - return instance.GetType(); - } - return null; + return instance?.GetType(); } } } diff --git a/src/Umbraco.Web/Mvc/MasterControllerFactory.cs b/src/Umbraco.Web/Mvc/MasterControllerFactory.cs index 3d1e450c93..30838e606a 100644 --- a/src/Umbraco.Web/Mvc/MasterControllerFactory.cs +++ b/src/Umbraco.Web/Mvc/MasterControllerFactory.cs @@ -54,8 +54,8 @@ namespace Umbraco.Web.Mvc { var factory = FactoryForRequest(requestContext); return factory != null - ? factory.CreateController(requestContext, controllerName) - : base.CreateController(requestContext, controllerName); + ? factory.CreateController(requestContext, controllerName) + : base.CreateController(requestContext, controllerName); } /// @@ -76,11 +76,9 @@ namespace Umbraco.Web.Mvc // an instance of the controller to figure out what it is. This is a work around for not having a breaking change for: // http://issues.umbraco.org/issue/U4-1726 - var umbFactory = factory as UmbracoControllerFactory; - if (umbFactory != null) - { + if (factory is UmbracoControllerFactory umbFactory) return umbFactory.GetControllerType(requestContext, controllerName); - } + //we have no choice but to instantiate the controller var instance = factory.CreateController(requestContext, controllerName); return instance?.GetType(); @@ -97,8 +95,8 @@ namespace Umbraco.Web.Mvc public override void ReleaseController(IController icontroller) { var released = false; - var controller = icontroller as Controller; - if (controller != null) + + if (icontroller is Controller controller) { var requestContext = controller.ControllerContext.RequestContext; var factory = FactoryForRequest(requestContext); @@ -108,6 +106,7 @@ namespace Umbraco.Web.Mvc released = true; } } + if (released == false) base.ReleaseController(icontroller); } diff --git a/src/Umbraco.Web/Mvc/RenderControllerFactory.cs b/src/Umbraco.Web/Mvc/RenderControllerFactory.cs index 8158cc8d22..c9734e24b0 100644 --- a/src/Umbraco.Web/Mvc/RenderControllerFactory.cs +++ b/src/Umbraco.Web/Mvc/RenderControllerFactory.cs @@ -10,7 +10,6 @@ namespace Umbraco.Web.Mvc /// public class RenderControllerFactory : UmbracoControllerFactory { - /// /// Determines whether this instance can handle the specified request. /// @@ -34,16 +33,13 @@ namespace Umbraco.Web.Mvc /// public override IController CreateController(RequestContext requestContext, string controllerName) { - var instance = base.CreateController(requestContext, controllerName); - var controllerInstance = instance as Controller; - if (controllerInstance != null) - { - //set the action invoker! - controllerInstance.ActionInvoker = new RenderActionInvoker(); - } - - return instance; + var instance = base.CreateController(requestContext, controllerName); + if (instance is Controller controllerInstance) + { + //set the action invoker! + controllerInstance.ActionInvoker = new RenderActionInvoker(); + } + return instance; } - } } diff --git a/src/Umbraco.Web/Mvc/UmbracoControllerFactory.cs b/src/Umbraco.Web/Mvc/UmbracoControllerFactory.cs index 330031976d..45ca1480c7 100644 --- a/src/Umbraco.Web/Mvc/UmbracoControllerFactory.cs +++ b/src/Umbraco.Web/Mvc/UmbracoControllerFactory.cs @@ -66,12 +66,10 @@ namespace Umbraco.Web.Mvc /// /// internal class OverridenDefaultControllerFactory : ContainerControllerFactory - // DefaultControllerFactory { public OverridenDefaultControllerFactory() : base(Current.Container) - { - } + { } public new IController GetControllerInstance(RequestContext requestContext, Type controllerType) { @@ -80,11 +78,9 @@ namespace Umbraco.Web.Mvc public new Type GetControllerType(RequestContext requestContext, string controllerName) { - if (controllerName.IsNullOrWhiteSpace()) - { - return null; - } - return base.GetControllerType(requestContext, controllerName); + return controllerName.IsNullOrWhiteSpace() + ? null + : base.GetControllerType(requestContext, controllerName); } } }