2010-06-21 Marek Habersack <mhabersack@novell.com>
[mcs.git] / class / System.Web.Mvc / System.Web.Mvc / ControllerBuilder.cs
blobc86f740fd42e62239cc9b5a4e43160546f346682
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 {
14 using System;
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
29 });
32 public static ControllerBuilder Current {
33 get {
34 return _instance;
38 public HashSet<string> DefaultNamespaces {
39 get {
40 return _namespaces;
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(
65 String.Format(
66 CultureInfo.CurrentUICulture,
67 MvcResources.ControllerBuilder_MissingIControllerFactory,
68 controllerFactoryType),
69 "controllerFactoryType");
72 _factoryThunk = delegate() {
73 try {
74 return (IControllerFactory)Activator.CreateInstance(controllerFactoryType);
76 catch (Exception ex) {
77 throw new InvalidOperationException(
78 String.Format(
79 CultureInfo.CurrentUICulture,
80 MvcResources.ControllerBuilder_ErrorCreatingControllerFactory,
81 controllerFactoryType),
82 ex);