Getting the controller type and instance seperately

--HG--
branch : dev
This commit is contained in:
Louis DeJardin
2010-11-09 20:31:37 -08:00
parent 0a7a88b535
commit 8674ff9f56
3 changed files with 44 additions and 13 deletions

View File

@@ -89,10 +89,13 @@ namespace Orchard.Environment.ShellBuilders {
}
foreach (var item in blueprint.Controllers) {
var serviceKey = (item.AreaName + "/" + item.ControllerName).ToLowerInvariant();
var serviceKeyName = (item.AreaName + "/" + item.ControllerName).ToLowerInvariant();
var serviceKeyType = item.Type;
RegisterType(builder, item)
.EnableDynamicProxy(dynamicProxyContext)
.Keyed<IController>(serviceKey)
.Keyed<IController>(serviceKeyName)
.Keyed<IController>(serviceKeyType)
.WithMetadata("ControllerType", item.Type)
.InstancePerDependency()
.OnActivating(e => {
var controller = e.Instance as Controller;

View File

@@ -1,12 +1,33 @@
using System;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Core;
using Autofac.Features.Metadata;
namespace Orchard.Mvc {
public interface IControllerType {
Type ControllerType { get; }
}
public class OrchardControllerFactory : DefaultControllerFactory {
public override IController CreateController(RequestContext requestContext, string controllerName) {
bool TryResolve<T>(RequestContext requestContext, object serviceKey, out T instance ) {
var workContext = requestContext.GetWorkContext();
if (workContext != null) {
var key = new KeyedService(serviceKey, typeof (T));
object value;
if (workContext.Resolve<ILifetimeScope>().TryResolve(key, out value)) {
instance = (T) value;
return true;
}
}
instance = default(T);
return false;
}
protected override Type GetControllerType(RequestContext requestContext, string controllerName) {
var routeData = requestContext.RouteData;
// Determine the area name for the request, and fall back to stock orchard controllers
@@ -15,18 +36,24 @@ namespace Orchard.Mvc {
// Service name pattern matches the identification strategy
var serviceKey = (areaName + "/" + controllerName).ToLowerInvariant();
// Now that the request container is known - try to resolve the controller
object controller;
var service = new KeyedService(serviceKey, typeof(IController));
var workContext = requestContext.GetWorkContext();
if (workContext != null &&
workContext.Resolve<ILifetimeScope>().TryResolve(service, out controller)) {
return (IController)controller;
// Now that the request container is known - try to resolve the controller information
Lazy<IController, IControllerType> info;
if (TryResolve(requestContext, serviceKey, out info)) {
return info.Metadata.ControllerType;
}
return base.CreateController(requestContext, controllerName);
// fail as appropriate for MVC's expectations
return null;
}
protected override IController GetControllerInstance(RequestContext requestContext, System.Type controllerType) {
IController controller;
if (TryResolve(requestContext, controllerType, out controller)) {
return controller;
}
// fail as appropriate for MVC's expectations
return null;
}
public static string GetAreaName(RouteBase route) {

View File

@@ -114,6 +114,7 @@
<HintPath>..\..\lib\linqnhibernate\NHibernate.Linq.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.ComponentModel.DataAnnotations">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>