This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
DefaultControllerPropertyActivator.cs
69 lines (58 loc) · 2.66 KB
/
DefaultControllerPropertyActivator.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
// 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.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc.Infrastructure;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNet.Mvc.Controllers
{
public class DefaultControllerPropertyActivator : IControllerPropertyActivator
{
private readonly IActionBindingContextAccessor _actionBindingContextAccessor;
private readonly ConcurrentDictionary<Type, PropertyActivator<Contexts>[]> _activateActions;
private readonly Func<Type, PropertyActivator<Contexts>[]> _getPropertiesToActivate;
public DefaultControllerPropertyActivator(IActionBindingContextAccessor actionBindingContextAccessor)
{
_actionBindingContextAccessor = actionBindingContextAccessor;
_activateActions = new ConcurrentDictionary<Type, PropertyActivator<Contexts>[]>();
_getPropertiesToActivate = GetPropertiesToActivate;
}
public void Activate(ActionContext actionContext, object controller)
{
var controllerType = controller.GetType();
var propertiesToActivate = _activateActions.GetOrAdd(
controllerType,
_getPropertiesToActivate);
var contexts = new Contexts()
{
ActionBindingContext = _actionBindingContextAccessor.ActionBindingContext,
ActionContext = actionContext,
};
for (var i = 0; i < propertiesToActivate.Length; i++)
{
var activateInfo = propertiesToActivate[i];
activateInfo.Activate(controller, contexts);
}
}
private PropertyActivator<Contexts>[] GetPropertiesToActivate(Type type)
{
IEnumerable<PropertyActivator<Contexts>> activators;
activators = PropertyActivator<Contexts>.GetPropertiesToActivate(
type,
typeof(ActionContextAttribute),
p => new PropertyActivator<Contexts>(p, c => c.ActionContext));
activators = activators.Concat(PropertyActivator<Contexts>.GetPropertiesToActivate(
type,
typeof(ActionBindingContextAttribute),
p => new PropertyActivator<Contexts>(p, c => c.ActionBindingContext)));
return activators.ToArray();
}
private struct Contexts
{
public ActionBindingContext ActionBindingContext;
public ActionContext ActionContext;
}
}
}