Issue #1754 - Change List to IList in application model

This commit is contained in:
Ryan Nowak 2015-01-05 12:36:13 -08:00
parent 5262dfd577
commit fb21b736ee
9 changed files with 59 additions and 35 deletions

View File

@ -48,7 +48,7 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
}
}
public List<IActionConstraintMetadata> ActionConstraints { get; private set; }
public IList<IActionConstraintMetadata> ActionConstraints { get; private set; }
public MethodInfo ActionMethod { get; }
@ -69,12 +69,12 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
public ControllerModel Controller { get; set; }
public List<IFilter> Filters { get; private set; }
public IList<IFilter> Filters { get; private set; }
public List<string> HttpMethods { get; private set; }
public IList<string> HttpMethods { get; private set; }
public List<ParameterModel> Parameters { get; private set; }
public IList<ParameterModel> Parameters { get; private set; }
public List<IRouteConstraintProvider> RouteConstraints { get; private set; }
public IList<IRouteConstraintProvider> RouteConstraints { get; private set; }
}
}

View File

@ -13,8 +13,8 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
Filters = new List<IFilter>();
}
public List<ControllerModel> Controllers { get; private set; }
public IList<ControllerModel> Controllers { get; private set; }
public List<IFilter> Filters { get; private set; }
public IList<IFilter> Filters { get; private set; }
}
}

View File

@ -44,9 +44,9 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
other.AttributeRoutes.Select(a => new AttributeRouteModel(a)));
}
public List<IActionConstraintMetadata> ActionConstraints { get; private set; }
public IList<IActionConstraintMetadata> ActionConstraints { get; private set; }
public List<ActionModel> Actions { get; private set; }
public IList<ActionModel> Actions { get; private set; }
/// <summary>
/// Gets or sets the <see cref="ApiExplorerModel"/> for this controller.
@ -55,7 +55,7 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
public ApplicationModel Application { get; set; }
public List<AttributeRouteModel> AttributeRoutes { get; private set; }
public IList<AttributeRouteModel> AttributeRoutes { get; private set; }
public IReadOnlyList<object> Attributes { get; }
@ -63,8 +63,8 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
public TypeInfo ControllerType { get; private set; }
public List<IFilter> Filters { get; private set; }
public IList<IFilter> Filters { get; private set; }
public List<IRouteConstraintProvider> RouteConstraints { get; private set; }
public IList<IRouteConstraintProvider> RouteConstraints { get; private set; }
}
}

View File

@ -254,8 +254,8 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
{
var actionModel = new ActionModel(methodInfo, attributes);
actionModel.ActionConstraints.AddRange(attributes.OfType<IActionConstraintMetadata>());
actionModel.Filters.AddRange(attributes.OfType<IFilter>());
AddRange(actionModel.ActionConstraints, attributes.OfType<IActionConstraintMetadata>());
AddRange(actionModel.Filters, attributes.OfType<IFilter>());
var actionName = attributes.OfType<ActionNameAttribute>().FirstOrDefault();
if (actionName?.Name != null)
@ -280,13 +280,13 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
}
var httpMethods = attributes.OfType<IActionHttpMethodProvider>();
actionModel.HttpMethods.AddRange(
AddRange(actionModel.HttpMethods,
httpMethods
.Where(a => a.HttpMethods != null)
.SelectMany(a => a.HttpMethods)
.Distinct());
actionModel.RouteConstraints.AddRange(attributes.OfType<IRouteConstraintProvider>());
AddRange(actionModel.RouteConstraints, attributes.OfType<IRouteConstraintProvider>());
var routeTemplateProvider =
attributes
@ -329,5 +329,13 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
routeTemplateProvider.Order == null &&
routeTemplateProvider.Name == null;
}
private static void AddRange<T>(IList<T> list, IEnumerable<T> items)
{
foreach (var item in items)
{
list.Add(item);
}
}
}
}

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNet.Mvc.Description;
@ -120,11 +121,12 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
typeInfo.Name.Substring(0, typeInfo.Name.Length - "Controller".Length) :
typeInfo.Name;
controllerModel.ActionConstraints.AddRange(attributes.OfType<IActionConstraintMetadata>());
controllerModel.Filters.AddRange(attributes.OfType<IFilter>());
controllerModel.RouteConstraints.AddRange(attributes.OfType<IRouteConstraintProvider>());
AddRange(controllerModel.ActionConstraints, attributes.OfType<IActionConstraintMetadata>());
AddRange(controllerModel.Filters, attributes.OfType<IFilter>());
AddRange(controllerModel.RouteConstraints, attributes.OfType<IRouteConstraintProvider>());
controllerModel.AttributeRoutes.AddRange(
AddRange(
controllerModel.AttributeRoutes,
attributes.OfType<IRouteTemplateProvider>().Select(rtp => new AttributeRouteModel(rtp)));
var apiVisibility = attributes.OfType<IApiDescriptionVisibilityProvider>().FirstOrDefault();
@ -141,5 +143,13 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
return controllerModel;
}
private static void AddRange<T>(IList<T> list, IEnumerable<T> items)
{
foreach (var item in items)
{
list.Add(item);
}
}
}
}

View File

@ -61,7 +61,10 @@ namespace Microsoft.AspNet.Mvc
public ApplicationModel BuildModel()
{
var applicationModel = new ApplicationModel();
applicationModel.Filters.AddRange(_globalFilters);
foreach (var filter in _globalFilters)
{
applicationModel.Filters.Add(filter);
}
var assemblies = _assemblyProvider.CandidateAssemblies;
var types = assemblies.SelectMany(a => a.DefinedTypes);

View File

@ -55,19 +55,19 @@ namespace Microsoft.AspNet.Mvc.Logging
/// The parameters of the action as <see cref="ParameterModelValues"/>.
/// See <see cref="ActionModel.Parameters"/>.
/// </summary>
public List<ParameterModelValues> Parameters { get; }
public IList<ParameterModelValues> Parameters { get; }
/// <summary>
/// The filters of the action as <see cref="FilterValues"/>.
/// See <see cref="ActionModel.Filters"/>.
/// </summary>
public List<FilterValues> Filters { get; }
public IList<FilterValues> Filters { get; }
/// <summary>
/// The route constraints on the controller as <see cref="RouteConstraintProviderValues"/>.
/// See <see cref="ControllerModel.RouteConstraints"/>.
/// </summary>
public List<RouteConstraintProviderValues> RouteConstraints { get; set; }
public IList<RouteConstraintProviderValues> RouteConstraints { get; set; }
/// <summary>
/// The attribute route model of the action as <see cref="AttributeRouteModelValues"/>.
@ -78,13 +78,13 @@ namespace Microsoft.AspNet.Mvc.Logging
/// <summary>
/// The http methods this action supports. See <see cref="ActionModel.HttpMethods"/>.
/// </summary>
public List<string> HttpMethods { get; }
public IList<string> HttpMethods { get; }
/// <summary>
/// The action constraints of the action as <see cref="ActionConstraintValues"/>.
/// See <see cref="ActionModel.ActionConstraints"/>.
/// </summary>
public List<ActionConstraintValues> ActionConstraints { get; }
public IList<ActionConstraintValues> ActionConstraints { get; }
public override string Format()
{

View File

@ -57,7 +57,10 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim
}
}
controller.Actions.AddRange(newActions);
foreach (var action in newActions)
{
controller.Actions.Add(action);
}
}
private bool IsActionAttributeRouted(ActionModel action)

View File

@ -576,7 +576,7 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
// Assert
var action = Assert.Single(actions);
Assert.Equal(new string[] { "GET" }, action.HttpMethods);
Assert.Equal<string>(new string[] { "GET" }, action.HttpMethods);
Assert.Equal("Products", action.AttributeRouteModel.Template);
}
@ -595,10 +595,10 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
Assert.Equal(2, actions.Count());
var action = Assert.Single(actions, a => a.AttributeRouteModel.Template == "Products");
Assert.Equal(new string[] { "GET", "POST" }, action.HttpMethods);
Assert.Equal<string>(new string[] { "GET", "POST" }, action.HttpMethods);
action = Assert.Single(actions, a => a.AttributeRouteModel.Template == "v2/Products");
Assert.Equal(new string[] { "GET", "POST" }, action.HttpMethods);
Assert.Equal<string>(new string[] { "GET", "POST" }, action.HttpMethods);
}
[Fact]
@ -616,13 +616,13 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
Assert.Equal(3, actions.Count());
var action = Assert.Single(actions, a => a.AttributeRouteModel.Template == "Products");
Assert.Equal(new string[] { "GET" }, action.HttpMethods);
Assert.Equal<string>(new string[] { "GET" }, action.HttpMethods);
action = Assert.Single(actions, a => a.AttributeRouteModel.Template == "v2/Products");
Assert.Equal(new string[] { "GET" }, action.HttpMethods);
Assert.Equal<string>(new string[] { "GET" }, action.HttpMethods);
action = Assert.Single(actions, a => a.AttributeRouteModel.Template == "Products/Buy");
Assert.Equal(new string[] { "POST" }, action.HttpMethods);
Assert.Equal<string>(new string[] { "POST" }, action.HttpMethods);
}
[Fact]
@ -640,10 +640,10 @@ namespace Microsoft.AspNet.Mvc.ApplicationModels
Assert.Equal(2, actions.Count());
var action = Assert.Single(actions, a => a.AttributeRouteModel?.Template == "Products");
Assert.Equal(new string[] { "POST" }, action.HttpMethods);
Assert.Equal<string>(new string[] { "POST" }, action.HttpMethods);
action = Assert.Single(actions, a => a.AttributeRouteModel?.Template == null);
Assert.Equal(new string[] { "GET" }, action.HttpMethods);
Assert.Equal<string>(new string[] { "GET" }, action.HttpMethods);
}
private class AccessibleActionModelBuilder : DefaultActionModelBuilder