This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ModelExpressionProvider.cs
71 lines (62 loc) · 2.65 KB
/
ModelExpressionProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq.Expressions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{
/// <summary>
/// A default implementation of <see cref="IModelMetadataProvider"/>.
/// </summary>
public class ModelExpressionProvider : IModelExpressionProvider
{
private readonly IModelMetadataProvider _modelMetadataProvider;
private readonly ExpressionTextCache _expressionTextCache;
/// <summary>
/// Creates a new <see cref="ModelExpressionProvider"/>.
/// </summary>
/// <param name="modelMetadataProvider">The <see cref="IModelMetadataProvider"/>.</param>
/// <param name="expressionTextCache">The <see cref="ExpressionTextCache"/>.</param>
public ModelExpressionProvider(
IModelMetadataProvider modelMetadataProvider,
#pragma warning disable PUB0001 // Pubternal type in public API
ExpressionTextCache expressionTextCache
#pragma warning restore PUB0001
)
{
if (modelMetadataProvider == null)
{
throw new ArgumentNullException(nameof(modelMetadataProvider));
}
if (expressionTextCache == null)
{
throw new ArgumentNullException(nameof(expressionTextCache));
}
_modelMetadataProvider = modelMetadataProvider;
_expressionTextCache = expressionTextCache;
}
/// <inheritdoc />
public ModelExpression CreateModelExpression<TModel, TValue>(
ViewDataDictionary<TModel> viewData,
Expression<Func<TModel, TValue>> expression)
{
if (viewData == null)
{
throw new ArgumentNullException(nameof(viewData));
}
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
var name = ExpressionHelper.GetExpressionText(expression, _expressionTextCache);
var modelExplorer = ExpressionMetadataProvider.FromLambdaExpression(expression, viewData, _modelMetadataProvider);
if (modelExplorer == null)
{
throw new InvalidOperationException(
Resources.FormatCreateModelExpression_NullModelMetadata(nameof(IModelMetadataProvider), name));
}
return new ModelExpression(name, modelExplorer);
}
}
}