This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
DefaultControllerFactory.cs
98 lines (87 loc) · 3.57 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
97
98
// 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 System.Reflection;
using Microsoft.AspNet.Mvc.Core;
namespace Microsoft.AspNet.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,
IEnumerable<IControllerPropertyActivator> propertyActivators)
{
_controllerActivator = controllerActivator;
_propertyActivators = propertyActivators.ToArray();
}
/// <summary>
/// The <see cref="IControllerActivator"/> used to create a controller.
/// </summary>
protected IControllerActivator ControllerActivator
{
get
{
return _controllerActivator;
}
}
/// <inheritdoc />
public virtual object CreateController(ActionContext actionContext)
{
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
var actionDescriptor = actionContext.ActionDescriptor as ControllerActionDescriptor;
if (actionDescriptor == null)
{
throw new ArgumentException(
Resources.FormatActionDescriptorMustBeBasedOnControllerAction(
typeof(ControllerActionDescriptor)),
nameof(actionContext));
}
var controllerType = actionDescriptor.ControllerTypeInfo.AsType();
var controllerTypeInfo = controllerType.GetTypeInfo();
if (controllerTypeInfo.IsValueType ||
controllerTypeInfo.IsInterface ||
controllerTypeInfo.IsAbstract ||
(controllerTypeInfo.IsGenericType && controllerTypeInfo.IsGenericTypeDefinition))
{
var message = Resources.FormatValueInterfaceAbstractOrOpenGenericTypesCannotBeActivated(
controllerType.FullName, GetType().FullName);
throw new InvalidOperationException(message);
}
var controller = _controllerActivator.Create(actionContext, controllerType);
foreach (var propertyActivator in _propertyActivators)
{
propertyActivator.Activate(actionContext, controller);
}
return controller;
}
/// <inheritdoc />
public virtual void ReleaseController(object controller)
{
var disposableController = controller as IDisposable;
if (disposableController != null)
{
disposableController.Dispose();
}
}
}
}