1
/* ****************************************************************************
3 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * This software is subject to the Microsoft Public License (Ms-PL).
6 * A copy of the license can be found in the license.htm file included
7 * in this distribution.
9 * You must not remove this notice, or any other, from this software.
11 * ***************************************************************************/
13 namespace System
.Web
.Mvc
{
15 using System
.Collections
.Generic
;
16 using System
.Diagnostics
.CodeAnalysis
;
17 using System
.Globalization
;
18 using System
.Web
.Mvc
.Resources
;
20 public class ControllerBuilder
{
22 private Func
<IControllerFactory
> _factoryThunk
;
23 private static ControllerBuilder _instance
= new ControllerBuilder();
24 private HashSet
<string> _namespaces
= new HashSet
<string>(StringComparer
.OrdinalIgnoreCase
);
26 public ControllerBuilder() {
27 SetControllerFactory(new DefaultControllerFactory() {
28 ControllerBuilder
= this
32 public static ControllerBuilder Current
{
38 public HashSet
<string> DefaultNamespaces
{
44 [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
45 Justification
= "Calling method multiple times might return different objects.")]
46 public IControllerFactory
GetControllerFactory() {
47 IControllerFactory controllerFactoryInstance
= _factoryThunk();
48 return controllerFactoryInstance
;
51 public void SetControllerFactory(IControllerFactory controllerFactory
) {
52 if (controllerFactory
== null) {
53 throw new ArgumentNullException("controllerFactory");
56 _factoryThunk
= () => controllerFactory
;
59 public void SetControllerFactory(Type controllerFactoryType
) {
60 if (controllerFactoryType
== null) {
61 throw new ArgumentNullException("controllerFactoryType");
63 if (!typeof(IControllerFactory
).IsAssignableFrom(controllerFactoryType
)) {
64 throw new ArgumentException(
66 CultureInfo
.CurrentUICulture
,
67 MvcResources
.ControllerBuilder_MissingIControllerFactory
,
68 controllerFactoryType
),
69 "controllerFactoryType");
72 _factoryThunk
= delegate() {
74 return (IControllerFactory
)Activator
.CreateInstance(controllerFactoryType
);
76 catch (Exception ex
) {
77 throw new InvalidOperationException(
79 CultureInfo
.CurrentUICulture
,
80 MvcResources
.ControllerBuilder_ErrorCreatingControllerFactory
,
81 controllerFactoryType
),