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
/
DefaultControllerFactory.cs
96 lines (83 loc) · 3.27 KB
/
DefaultControllerFactory.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
93
94
95
96
// 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.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.Internal;
namespace Microsoft.AspNetCore.Mvc.Controllers
{
/// <summary>
/// Default implementation for <see cref="IControllerFactory"/>.
/// </summary>
public class DefaultControllerFactory : IControllerFactory
{
private readonly IControllerActivator _controllerActivator;
private readonly IControllerPropertyActivator[] _propertyActivators;
/// <summary>
/// Initializes a new instance of <see cref="DefaultControllerFactory"/>.
/// </summary>
/// <param name="controllerActivator">
/// <see cref="IControllerActivator"/> used to create controller instances.
/// </param>
/// <param name="propertyActivators">
/// A set of <see cref="IControllerPropertyActivator"/> instances used to initialize controller
/// properties.
/// </param>
public DefaultControllerFactory(
IControllerActivator controllerActivator,
#pragma warning disable PUB0001 // Pubternal type in public API
IEnumerable<IControllerPropertyActivator> propertyActivators
#pragma warning restore PUB0001
)
{
if (controllerActivator == null)
{
throw new ArgumentNullException(nameof(controllerActivator));
}
if (propertyActivators == null)
{
throw new ArgumentNullException(nameof(propertyActivators));
}
_controllerActivator = controllerActivator;
_propertyActivators = propertyActivators.ToArray();
}
/// <summary>
/// The <see cref="IControllerActivator"/> used to create a controller.
/// </summary>
protected IControllerActivator ControllerActivator => _controllerActivator;
/// <inheritdoc />
public virtual object CreateController(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (context.ActionDescriptor == null)
{
throw new ArgumentException(Resources.FormatPropertyOfTypeCannotBeNull(
nameof(ControllerContext.ActionDescriptor),
nameof(ControllerContext)));
}
var controller = _controllerActivator.Create(context);
foreach (var propertyActivator in _propertyActivators)
{
propertyActivator.Activate(context, controller);
}
return controller;
}
/// <inheritdoc />
public virtual void ReleaseController(ControllerContext context, object controller)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (controller == null)
{
throw new ArgumentNullException(nameof(controller));
}
_controllerActivator.Release(context, controller);
}
}
}