My favorites | Sign in
Logo
                
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
99
100
101
102
103
104
105
106
// This software is part of the Autofac IoC container
// Copyright (c) 2007 - 2008 Autofac Contributors
// http://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web;

namespace Autofac.Integration.Web.Mvc
{
/// <summary>
/// An MS-MVC controller factory that returns controllers built by an
/// Autofac IoC container scoped according to the current request.
/// </summary>
public class AutofacControllerFactory : IControllerFactory
{
IContainerProvider _containerProvider;
IControllerIdentificationStrategy _controllerIdentificationStrategy;

/// <summary>
/// Initializes a new instance of the <see cref="AutofacControllerFactory"/> class.
/// </summary>
/// <param name="containerProvider">The container provider.</param>
public AutofacControllerFactory(IContainerProvider containerProvider)
: this(containerProvider, new DefaultControllerIdentificationStrategy())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AutofacControllerFactory"/> class.
/// </summary>
/// <param name="containerProvider">The container provider.</param>
/// <param name="controllerIdentificationStrategy">The controller identification strategy.</param>
public AutofacControllerFactory(
IContainerProvider containerProvider,
IControllerIdentificationStrategy controllerIdentificationStrategy)
{
if (containerProvider == null)
throw new ArgumentNullException("containerProvider");

if (controllerIdentificationStrategy == null)
throw new ArgumentNullException("controllerIdentificationStrategy");

_containerProvider = containerProvider;
_controllerIdentificationStrategy = controllerIdentificationStrategy;
}

/// <summary>
/// Creates the controller.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="controllerName">Name of the controller.</param>
/// <returns>The controller.</returns>
public virtual IController CreateController(RequestContext context, string controllerName)
{
if (context == null)
throw new ArgumentNullException("context");

if (controllerName == null)
throw new ArgumentNullException("controllerName");

var controllerService = _controllerIdentificationStrategy
.ServiceForControllerName(controllerName);

object controller = null;
if (_containerProvider.RequestLifetime.TryResolve(controllerService, out controller))
return (IController)controller;
else
throw new HttpException(404,
string.Format(AutofacControllerFactoryResources.NotFound,
controllerService,
controllerName,
context.HttpContext.Request.Path));
}

/// <summary>
/// Releases the controller. Unecessary in an Autofac-managed application.
/// </summary>
/// <param name="controller">The controller.</param>
public void ReleaseController(IController controller)
{
}
}
}
Show details Hide details

Change log

r606 by nicholas.blumhardt on Sep 11, 2009   Diff
Merged 2.0-experimental into trunk as 2.1.
Large number of TODOs that must be
addressed in the coming weeks - trunk will
be unstable for the immediate future.
Go to: 
Project members, sign in to write a code review

Older revisions

r532 by nicholas.blumhardt on Apr 04, 2009   Diff
Fixed  issue 113 : controller factory
not handling 404s.
r487 by nicholas.blumhardt on Mar 03, 2009   Diff
Prepared for 1.4 release. MVC Beta 2,
removed .NET 2 support, merged MVC and
Web DLLs.
All revisions of this file

File info

Size: 4573 bytes, 106 lines
Hosted by Google Code