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
/
ViewDataDictionaryControllerPropertyActivator.cs
89 lines (76 loc) · 3.4 KB
/
ViewDataDictionaryControllerPropertyActivator.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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.Collections.Concurrent;
using System.Threading;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{
public class ViewDataDictionaryControllerPropertyActivator : IControllerPropertyActivator
{
private readonly Func<Type, PropertyActivator<ControllerContext>[]> _getPropertiesToActivate;
private readonly IModelMetadataProvider _modelMetadataProvider;
private ConcurrentDictionary<Type, PropertyActivator<ControllerContext>[]> _activateActions;
private bool _initialized;
private object _initializeLock = new object();
public ViewDataDictionaryControllerPropertyActivator(IModelMetadataProvider modelMetadataProvider)
{
_modelMetadataProvider = modelMetadataProvider;
_getPropertiesToActivate = GetPropertiesToActivate;
}
public void Activate(ControllerContext actionContext, object controller)
{
LazyInitializer.EnsureInitialized(
ref _activateActions,
ref _initialized,
ref _initializeLock);
var controllerType = controller.GetType();
var propertiesToActivate = _activateActions.GetOrAdd(
controllerType,
_getPropertiesToActivate);
for (var i = 0; i < propertiesToActivate.Length; i++)
{
var activateInfo = propertiesToActivate[i];
activateInfo.Activate(controller, actionContext);
}
}
public Action<ControllerContext, object> GetActivatorDelegate(ControllerActionDescriptor actionDescriptor)
{
var controllerType = actionDescriptor.ControllerTypeInfo?.AsType();
if (controllerType == null)
{
throw new ArgumentException(Resources.FormatPropertyOfTypeCannotBeNull(
nameof(actionDescriptor.ControllerTypeInfo),
nameof(actionDescriptor)),
nameof(actionDescriptor));
}
var propertiesToActivate = GetPropertiesToActivate(controllerType);
void Activate(ControllerContext controllerContext, object controller)
{
for (var i = 0; i < propertiesToActivate.Length; i++)
{
var activateInfo = propertiesToActivate[i];
activateInfo.Activate(controller, controllerContext);
}
}
return Activate;
}
private PropertyActivator<ControllerContext>[] GetPropertiesToActivate(Type type)
{
var activators = PropertyActivator<ControllerContext>.GetPropertiesToActivate(
type,
typeof(ViewDataDictionaryAttribute),
p => new PropertyActivator<ControllerContext>(p, GetViewDataDictionary));
return activators;
}
private ViewDataDictionary GetViewDataDictionary(ControllerContext context)
{
return new ViewDataDictionary(
_modelMetadataProvider,
context.ModelState);
}
}
}