mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-12-03 03:58:13 +08:00
Getting the controller type and instance seperately
--HG-- branch : dev
This commit is contained in:
@@ -89,10 +89,13 @@ namespace Orchard.Environment.ShellBuilders {
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach (var item in blueprint.Controllers) {
|
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)
|
RegisterType(builder, item)
|
||||||
.EnableDynamicProxy(dynamicProxyContext)
|
.EnableDynamicProxy(dynamicProxyContext)
|
||||||
.Keyed<IController>(serviceKey)
|
.Keyed<IController>(serviceKeyName)
|
||||||
|
.Keyed<IController>(serviceKeyType)
|
||||||
|
.WithMetadata("ControllerType", item.Type)
|
||||||
.InstancePerDependency()
|
.InstancePerDependency()
|
||||||
.OnActivating(e => {
|
.OnActivating(e => {
|
||||||
var controller = e.Instance as Controller;
|
var controller = e.Instance as Controller;
|
||||||
|
|||||||
@@ -1,12 +1,33 @@
|
|||||||
|
using System;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using System.Web.Routing;
|
using System.Web.Routing;
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using Autofac.Core;
|
using Autofac.Core;
|
||||||
|
using Autofac.Features.Metadata;
|
||||||
|
|
||||||
namespace Orchard.Mvc {
|
namespace Orchard.Mvc {
|
||||||
|
public interface IControllerType {
|
||||||
|
Type ControllerType { get; }
|
||||||
|
}
|
||||||
|
|
||||||
public class OrchardControllerFactory : DefaultControllerFactory {
|
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;
|
var routeData = requestContext.RouteData;
|
||||||
|
|
||||||
// Determine the area name for the request, and fall back to stock orchard controllers
|
// 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
|
// Service name pattern matches the identification strategy
|
||||||
var serviceKey = (areaName + "/" + controllerName).ToLowerInvariant();
|
var serviceKey = (areaName + "/" + controllerName).ToLowerInvariant();
|
||||||
|
|
||||||
// Now that the request container is known - try to resolve the controller
|
// Now that the request container is known - try to resolve the controller information
|
||||||
object controller;
|
Lazy<IController, IControllerType> info;
|
||||||
var service = new KeyedService(serviceKey, typeof(IController));
|
if (TryResolve(requestContext, serviceKey, out info)) {
|
||||||
|
return info.Metadata.ControllerType;
|
||||||
var workContext = requestContext.GetWorkContext();
|
|
||||||
|
|
||||||
if (workContext != null &&
|
|
||||||
workContext.Resolve<ILifetimeScope>().TryResolve(service, out controller)) {
|
|
||||||
return (IController)controller;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
public static string GetAreaName(RouteBase route) {
|
||||||
|
|||||||
@@ -114,6 +114,7 @@
|
|||||||
<HintPath>..\..\lib\linqnhibernate\NHibernate.Linq.dll</HintPath>
|
<HintPath>..\..\lib\linqnhibernate\NHibernate.Linq.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.ComponentModel.Composition" />
|
||||||
<Reference Include="System.ComponentModel.DataAnnotations">
|
<Reference Include="System.ComponentModel.DataAnnotations">
|
||||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
|||||||
Reference in New Issue
Block a user