Add extension method for IParameterModelConvention. (#5640)
This commit is contained in:
parent
a42006d295
commit
9146fce4ec
|
|
@ -47,18 +47,82 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
this IList<IApplicationModelConvention> conventions,
|
this IList<IApplicationModelConvention> conventions,
|
||||||
IActionModelConvention actionModelConvention)
|
IActionModelConvention actionModelConvention)
|
||||||
{
|
{
|
||||||
|
if (conventions == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(conventions));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actionModelConvention == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(actionModelConvention));
|
||||||
|
}
|
||||||
|
|
||||||
conventions.Add(new ActionApplicationModelConvention(actionModelConvention));
|
conventions.Add(new ActionApplicationModelConvention(actionModelConvention));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a <see cref="IParameterModelConvention"/> to all the parameters in the application.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="conventions">The list of <see cref="IApplicationModelConvention"/>
|
||||||
|
/// in <see cref="AspNetCore.Mvc.MvcOptions"/>.</param>
|
||||||
|
/// <param name="parameterModelConvention">The <see cref="IParameterModelConvention"/> which needs to be
|
||||||
|
/// added.</param>
|
||||||
|
public static void Add(
|
||||||
|
this IList<IApplicationModelConvention> conventions,
|
||||||
|
IParameterModelConvention parameterModelConvention)
|
||||||
|
{
|
||||||
|
if (conventions == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(conventions));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parameterModelConvention == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(parameterModelConvention));
|
||||||
|
}
|
||||||
|
|
||||||
|
conventions.Add(new ParameterApplicationModelConvention(parameterModelConvention));
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ParameterApplicationModelConvention : IApplicationModelConvention
|
||||||
|
{
|
||||||
|
private IParameterModelConvention _parameterModelConvention;
|
||||||
|
|
||||||
|
public ParameterApplicationModelConvention(IParameterModelConvention parameterModelConvention)
|
||||||
|
{
|
||||||
|
if (parameterModelConvention == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(parameterModelConvention));
|
||||||
|
}
|
||||||
|
|
||||||
|
_parameterModelConvention = parameterModelConvention;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void Apply(ApplicationModel application)
|
||||||
|
{
|
||||||
|
if (application == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(application));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var controller in application.Controllers)
|
||||||
|
{
|
||||||
|
foreach (var action in controller.Actions)
|
||||||
|
{
|
||||||
|
foreach (var parameter in action.Parameters)
|
||||||
|
{
|
||||||
|
_parameterModelConvention.Apply(parameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class ActionApplicationModelConvention : IApplicationModelConvention
|
private class ActionApplicationModelConvention : IApplicationModelConvention
|
||||||
{
|
{
|
||||||
private IActionModelConvention _actionModelConvention;
|
private IActionModelConvention _actionModelConvention;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of <see cref="ActionApplicationModelConvention"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="actionModelConvention">The action convention to be applied on all actions
|
|
||||||
/// in the application.</param>
|
|
||||||
public ActionApplicationModelConvention(IActionModelConvention actionModelConvention)
|
public ActionApplicationModelConvention(IActionModelConvention actionModelConvention)
|
||||||
{
|
{
|
||||||
if (actionModelConvention == null)
|
if (actionModelConvention == null)
|
||||||
|
|
@ -91,11 +155,6 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
{
|
{
|
||||||
private IControllerModelConvention _controllerModelConvention;
|
private IControllerModelConvention _controllerModelConvention;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of <see cref="ControllerApplicationModelConvention"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="controllerConvention">The controller convention to be applied on all controllers
|
|
||||||
/// in the application.</param>
|
|
||||||
public ControllerApplicationModelConvention(IControllerModelConvention controllerConvention)
|
public ControllerApplicationModelConvention(IControllerModelConvention controllerConvention)
|
||||||
{
|
{
|
||||||
if (controllerConvention == null)
|
if (controllerConvention == null)
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,35 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
{
|
{
|
||||||
public class ApplicationModelConventionExtensionsTest
|
public class ApplicationModelConventionExtensionsTest
|
||||||
{
|
{
|
||||||
|
[Fact]
|
||||||
|
public void DefaultParameterModelConvention_AppliesToAllParametersInApp()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var app = new ApplicationModel();
|
||||||
|
app.Controllers.Add(new ControllerModel(typeof(HelloController).GetTypeInfo(), new List<object>()));
|
||||||
|
app.Controllers.Add(new ControllerModel(typeof(WorldController).GetTypeInfo(), new List<object>()));
|
||||||
|
|
||||||
|
var options = new MvcOptions();
|
||||||
|
options.Conventions.Add(new SimpleParameterConvention());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
options.Conventions[0].Apply(app);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
foreach (var controller in app.Controllers)
|
||||||
|
{
|
||||||
|
foreach (var action in controller.Actions)
|
||||||
|
{
|
||||||
|
foreach (var parameter in action.Parameters)
|
||||||
|
{
|
||||||
|
var kvp = Assert.Single(parameter.Properties);
|
||||||
|
Assert.Equal("TestProperty", kvp.Key);
|
||||||
|
Assert.Equal("TestValue", kvp.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void DefaultActionModelConvention_AppliesToAllActionsInApp()
|
public void DefaultActionModelConvention_AppliesToAllActionsInApp()
|
||||||
{
|
{
|
||||||
|
|
@ -31,7 +60,9 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
{
|
{
|
||||||
foreach (var action in controller.Actions)
|
foreach (var action in controller.Actions)
|
||||||
{
|
{
|
||||||
Assert.True(action.Properties.ContainsKey("TestProperty"));
|
var kvp = Assert.Single(action.Properties);
|
||||||
|
Assert.Equal("TestProperty", kvp.Key);
|
||||||
|
Assert.Equal("TestValue", kvp.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -52,7 +83,9 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
// Assert
|
// Assert
|
||||||
foreach (var controller in app.Controllers)
|
foreach (var controller in app.Controllers)
|
||||||
{
|
{
|
||||||
Assert.True(controller.Properties.ContainsKey("TestProperty"));
|
var kvp = Assert.Single(controller.Properties);
|
||||||
|
Assert.Equal("TestProperty", kvp.Key);
|
||||||
|
Assert.Equal("TestValue", kvp.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -72,6 +105,14 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class SimpleParameterConvention : IParameterModelConvention
|
||||||
|
{
|
||||||
|
public void Apply(ParameterModel parameter)
|
||||||
|
{
|
||||||
|
parameter.Properties.Add("TestProperty", "TestValue");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class SimpleActionConvention : IActionModelConvention
|
private class SimpleActionConvention : IActionModelConvention
|
||||||
{
|
{
|
||||||
public void Apply(ActionModel action)
|
public void Apply(ActionModel action)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue