From 803f447686b82596a7bab068a87ea76eb3841de8 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 26 Feb 2014 14:51:19 -0800 Subject: [PATCH] Use INestedProvider for ADP and AIP --- .../MvcServices.cs | 10 ++++-- .../ActionDescriptorProviderContext.cs | 15 ++++++++ .../ActionInvokerFactory.cs | 18 +++++----- .../ActionInvokerProvider.cs | 17 ++++++--- .../ActionInvokerProviderContext.cs | 21 +++++++++++ .../DefaultActionSelector.cs | 14 +++++--- .../IActionDescriptorProvider.cs | 6 ++-- .../IActionInvokerProvider.cs | 7 ++-- ...TypeMethodBasedActionDescriptorProvider.cs | 35 +++++++------------ 9 files changed, 93 insertions(+), 50 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc/ActionDescriptorProviderContext.cs create mode 100644 src/Microsoft.AspNet.Mvc/ActionInvokerProviderContext.cs diff --git a/src/Microsoft.AspNet.Mvc.Startup/MvcServices.cs b/src/Microsoft.AspNet.Mvc.Startup/MvcServices.cs index e07fd8eab3..15a59966ff 100644 --- a/src/Microsoft.AspNet.Mvc.Startup/MvcServices.cs +++ b/src/Microsoft.AspNet.Mvc.Startup/MvcServices.cs @@ -1,4 +1,5 @@ using Microsoft.AspNet.DependencyInjection; +using Microsoft.AspNet.DependencyInjection.NestedProviders; using Microsoft.AspNet.FileSystems; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.Razor; @@ -19,14 +20,11 @@ namespace Microsoft.AspNet.Mvc.Startup Add(); Add(); Add(); - Add(); Add(); Add(); Add(); - Add(); Add(); Add(); - AddInstance(new PhysicalFileSystem(appRoot)); AddInstance(new MvcRazorHost(typeof(RazorView).FullName)); @@ -36,6 +34,12 @@ namespace Microsoft.AspNet.Mvc.Startup Add(); Add(); Add(); + + // This is temporary until DI has some magic for it + Add, NestedProviderManager>(); + Add, NestedProviderManager>(); + Add, TypeMethodBasedActionDescriptorProvider>(); + Add, ActionInvokerProvider>(); } private void Add() where TU : T diff --git a/src/Microsoft.AspNet.Mvc/ActionDescriptorProviderContext.cs b/src/Microsoft.AspNet.Mvc/ActionDescriptorProviderContext.cs new file mode 100644 index 0000000000..2bbc1526f6 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc/ActionDescriptorProviderContext.cs @@ -0,0 +1,15 @@ + +using System.Collections.Generic; + +namespace Microsoft.AspNet.Mvc +{ + public class ActionDescriptorProviderContext + { + public ActionDescriptorProviderContext() + { + ActionDescriptors = new List(); + } + + public List ActionDescriptors { get; private set; } + } +} diff --git a/src/Microsoft.AspNet.Mvc/ActionInvokerFactory.cs b/src/Microsoft.AspNet.Mvc/ActionInvokerFactory.cs index 4bd7f8e5c1..642911a8c9 100644 --- a/src/Microsoft.AspNet.Mvc/ActionInvokerFactory.cs +++ b/src/Microsoft.AspNet.Mvc/ActionInvokerFactory.cs @@ -1,23 +1,21 @@ -namespace Microsoft.AspNet.Mvc +using Microsoft.AspNet.DependencyInjection; + +namespace Microsoft.AspNet.Mvc { public class ActionInvokerFactory : IActionInvokerFactory { - private readonly IActionResultFactory _actionResultFactory; - private readonly IActionInvokerProvider _actionInvokerProvider; - private readonly IActionDescriptorProvider _routeContextProvider; + private readonly INestedProviderManager _actionInvokerProvider; - public ActionInvokerFactory(IActionResultFactory actionResultFactory, - IActionDescriptorProvider actionDescriptorProvider, - IActionInvokerProvider actionInvokerProvider) + public ActionInvokerFactory(INestedProviderManager actionInvokerProvider) { - _actionResultFactory = actionResultFactory; - _routeContextProvider = actionDescriptorProvider; _actionInvokerProvider = actionInvokerProvider; } public IActionInvoker CreateInvoker(ActionContext actionContext) { - return _actionInvokerProvider.GetInvoker(actionContext); + var context = new ActionInvokerProviderContext(actionContext); + _actionInvokerProvider.Invoke(context); + return context.ActionInvoker; } } } diff --git a/src/Microsoft.AspNet.Mvc/ActionInvokerProvider.cs b/src/Microsoft.AspNet.Mvc/ActionInvokerProvider.cs index e5bc20e074..7ae935d0b4 100644 --- a/src/Microsoft.AspNet.Mvc/ActionInvokerProvider.cs +++ b/src/Microsoft.AspNet.Mvc/ActionInvokerProvider.cs @@ -17,21 +17,28 @@ namespace Microsoft.AspNet.Mvc _serviceProvider = serviceProvider; } - public IActionInvoker GetInvoker(ActionContext actionContext) + public int Order { - var ad = actionContext.ActionDescriptor as TypeMethodBasedActionDescriptor; + get { return 0; } + } + + public void Invoke(ActionInvokerProviderContext context, Action callNext) + { + var ad = context.ActionContext.ActionDescriptor as TypeMethodBasedActionDescriptor; if (ad != null) { - return new TypeMethodBasedActionInvoker( - actionContext, + context.ActionInvoker = new TypeMethodBasedActionInvoker( + context.ActionContext, ad, _actionResultFactory, _controllerFactory, _serviceProvider); } - return null; + callNext(); } + + } } diff --git a/src/Microsoft.AspNet.Mvc/ActionInvokerProviderContext.cs b/src/Microsoft.AspNet.Mvc/ActionInvokerProviderContext.cs new file mode 100644 index 0000000000..9e849dfe7b --- /dev/null +++ b/src/Microsoft.AspNet.Mvc/ActionInvokerProviderContext.cs @@ -0,0 +1,21 @@ +using System; + +namespace Microsoft.AspNet.Mvc +{ + public class ActionInvokerProviderContext + { + public ActionInvokerProviderContext(ActionContext actionContext) + { + if (actionContext == null) + { + throw new ArgumentNullException("actionContext"); + } + + ActionContext = actionContext; + } + + public ActionContext ActionContext { get; private set; } + + public IActionInvoker ActionInvoker { get; set; } + } +} diff --git a/src/Microsoft.AspNet.Mvc/DefaultActionSelector.cs b/src/Microsoft.AspNet.Mvc/DefaultActionSelector.cs index 011a109371..0a79115bcb 100644 --- a/src/Microsoft.AspNet.Mvc/DefaultActionSelector.cs +++ b/src/Microsoft.AspNet.Mvc/DefaultActionSelector.cs @@ -2,18 +2,21 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNet.DependencyInjection; using Microsoft.AspNet.Mvc.ModelBinding; namespace Microsoft.AspNet.Mvc { public class DefaultActionSelector : IActionSelector { - private readonly IEnumerable _actionDescriptorProviders; + private readonly INestedProviderManager _actionDescriptorProvider; private readonly IEnumerable _valueProviderFactory; - public DefaultActionSelector(IEnumerable actionDescriptorProviders, IEnumerable valueProviderFactories) + public DefaultActionSelector( + INestedProviderManager actionDescriptorProvider, + IEnumerable valueProviderFactories) { - _actionDescriptorProviders = actionDescriptorProviders; + _actionDescriptorProvider = actionDescriptorProvider; _valueProviderFactory = valueProviderFactories; } @@ -24,7 +27,10 @@ namespace Microsoft.AspNet.Mvc throw new ArgumentNullException("context"); } - var allDescriptors = _actionDescriptorProviders.SelectMany(d => d.GetDescriptors()); + var actionDescriptorProviderContext = new ActionDescriptorProviderContext(); + _actionDescriptorProvider.Invoke(actionDescriptorProviderContext); + + var allDescriptors = actionDescriptorProviderContext.Results; var matching = allDescriptors.Where(ad => Match(ad, context)).ToList(); if (matching.Count == 0) diff --git a/src/Microsoft.AspNet.Mvc/IActionDescriptorProvider.cs b/src/Microsoft.AspNet.Mvc/IActionDescriptorProvider.cs index 674eba433d..8def55629e 100644 --- a/src/Microsoft.AspNet.Mvc/IActionDescriptorProvider.cs +++ b/src/Microsoft.AspNet.Mvc/IActionDescriptorProvider.cs @@ -1,9 +1,9 @@ -using System.Collections.Generic; + +using Microsoft.AspNet.DependencyInjection; namespace Microsoft.AspNet.Mvc { - public interface IActionDescriptorProvider + public interface IActionDescriptorProvider : INestedProvider { - IEnumerable GetDescriptors(); } } diff --git a/src/Microsoft.AspNet.Mvc/IActionInvokerProvider.cs b/src/Microsoft.AspNet.Mvc/IActionInvokerProvider.cs index 47aed560f0..0490235b26 100644 --- a/src/Microsoft.AspNet.Mvc/IActionInvokerProvider.cs +++ b/src/Microsoft.AspNet.Mvc/IActionInvokerProvider.cs @@ -1,7 +1,8 @@ -namespace Microsoft.AspNet.Mvc +using Microsoft.AspNet.DependencyInjection; + +namespace Microsoft.AspNet.Mvc { - public interface IActionInvokerProvider + public interface IActionInvokerProvider : INestedProvider { - IActionInvoker GetInvoker(ActionContext actionContext); } } diff --git a/src/Microsoft.AspNet.Mvc/TypeMethodBasedActionDescriptorProvider.cs b/src/Microsoft.AspNet.Mvc/TypeMethodBasedActionDescriptorProvider.cs index 8674d4368c..252e51185f 100644 --- a/src/Microsoft.AspNet.Mvc/TypeMethodBasedActionDescriptorProvider.cs +++ b/src/Microsoft.AspNet.Mvc/TypeMethodBasedActionDescriptorProvider.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -22,6 +23,17 @@ namespace Microsoft.AspNet.Mvc _parameterDescriptorFactory = parameterDescriptorFactory; } + public int Order + { + get { return 0; } + } + + public void Invoke(ActionDescriptorProviderContext context, Action callNext) + { + context.ActionDescriptors.AddRange(GetDescriptors()); + callNext(); + } + public IEnumerable GetDescriptors() { var assemblies = _controllerAssemblyProvider.Assemblies; @@ -84,26 +96,5 @@ namespace Microsoft.AspNet.Mvc return ad; } - - private static void ApplyRest(TypeMethodBasedActionDescriptor descriptor, IEnumerable httpMethods) - { - - descriptor.RouteConstraints.Add(new RouteDataActionConstraint("action", RouteKeyHandling.DenyKey)); - } - - private static void ApplyRpc(TypeMethodBasedActionDescriptor descriptor, ActionInfo convention) - { - - var methods = convention.HttpMethods; - - // rest action require specific methods, but RPC actions do not. - if (methods != null) - { - descriptor.MethodConstraints = new List - { - new HttpMethodConstraint(methods) - }; - } - } } }