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
/
DefaultViewComponentFactory.cs
92 lines (76 loc) · 3.15 KB
/
DefaultViewComponentFactory.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
90
91
92
// 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.Reflection;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Mvc.ViewComponents
{
/// <summary>
/// Default implementation for <see cref="IViewComponentFactory"/>.
/// </summary>
public class DefaultViewComponentFactory : IViewComponentFactory
{
private readonly IViewComponentActivator _activator;
private readonly Func<Type, PropertyActivator<ViewComponentContext>[]> _getPropertiesToActivate;
private readonly ConcurrentDictionary<Type, PropertyActivator<ViewComponentContext>[]> _injectActions;
/// <summary>
/// Creates a new instance of <see cref="DefaultViewComponentFactory"/>
/// </summary>
/// <param name="activator">
/// The <see cref="IViewComponentActivator"/> used to create new view component instances.
/// </param>
public DefaultViewComponentFactory(IViewComponentActivator activator)
{
if (activator == null)
{
throw new ArgumentNullException(nameof(activator));
}
_activator = activator;
_getPropertiesToActivate = type => PropertyActivator<ViewComponentContext>.GetPropertiesToActivate(
type,
typeof(ViewComponentContextAttribute),
CreateActivateInfo);
_injectActions = new ConcurrentDictionary<Type, PropertyActivator<ViewComponentContext>[]>();
}
/// <inheritdoc />
public object CreateViewComponent(ViewComponentContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var component = _activator.Create(context);
InjectProperties(context, component);
return component;
}
private void InjectProperties(ViewComponentContext context, object viewComponent)
{
var propertiesToActivate = _injectActions.GetOrAdd(
viewComponent.GetType(),
_getPropertiesToActivate);
for (var i = 0; i < propertiesToActivate.Length; i++)
{
var activateInfo = propertiesToActivate[i];
activateInfo.Activate(viewComponent, context);
}
}
private static PropertyActivator<ViewComponentContext> CreateActivateInfo(PropertyInfo property)
{
return new PropertyActivator<ViewComponentContext>(property, context => context);
}
/// <inheritdoc />
public void ReleaseViewComponent(ViewComponentContext context, object component)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (component == null)
{
throw new ArgumentNullException(nameof(component));
}
_activator.Release(context, component);
}
}
}