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