From f430c6b44d32736857ab5e3499d0adbf7dd6df2c Mon Sep 17 00:00:00 2001 From: Louis DeJardin Date: Mon, 30 Aug 2010 17:10:05 -0700 Subject: [PATCH] Removing ShapeBinding delegate type in favor of Func Was slightly confusing - no other delegates are typed, making the named ShapeBinding look like a class. --HG-- branch : mvc3p1 --- .../DisplayManagement/Descriptors/Interfaces.cs | 10 +++++----- .../ShapeAttributeBindingStrategy.cs | 11 ++++++++--- .../ShapeTemplateBindingStrategy.cs | 3 ++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Orchard/DisplayManagement/Descriptors/Interfaces.cs b/src/Orchard/DisplayManagement/Descriptors/Interfaces.cs index 919f53cc6..fec859525 100644 --- a/src/Orchard/DisplayManagement/Descriptors/Interfaces.cs +++ b/src/Orchard/DisplayManagement/Descriptors/Interfaces.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Web; using Orchard.DisplayManagement.Implementation; using Orchard.Environment.Extensions.Models; @@ -24,10 +25,9 @@ namespace Orchard.DisplayManagement.Descriptors { public class ShapeDescriptor { public string ShapeType { get; set; } - public ShapeBinding Binding { get; set; } + public Func Binding { get; set; } } - public delegate object ShapeBinding(DisplayContext displayContext); public class ShapeTableBuilder { readonly IList _descriptorBuilders = new List(); @@ -89,11 +89,11 @@ namespace Orchard.DisplayManagement.Descriptors { return this; } - public ShapeDescriptorAlterationBuilder BoundAs(Func binder) { + public ShapeDescriptorAlterationBuilder BoundAs(Func> binder) { // schedule the configuration return Configure(descriptor => { - - ShapeBinding target = null; + + Func target = null; // announce the binding, which may be reconfigured before it's used descriptor.Binding = displayContext => { diff --git a/src/Orchard/DisplayManagement/Descriptors/ShapeAttributeStrategy/ShapeAttributeBindingStrategy.cs b/src/Orchard/DisplayManagement/Descriptors/ShapeAttributeStrategy/ShapeAttributeBindingStrategy.cs index 7efeeef48..d21f0824a 100644 --- a/src/Orchard/DisplayManagement/Descriptors/ShapeAttributeStrategy/ShapeAttributeBindingStrategy.cs +++ b/src/Orchard/DisplayManagement/Descriptors/ShapeAttributeStrategy/ShapeAttributeBindingStrategy.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; +using System.Web; using Autofac; using Autofac.Core; using Microsoft.CSharp.RuntimeBinder; @@ -34,7 +35,7 @@ namespace Orchard.DisplayManagement.Descriptors.ShapeAttributeStrategy { } } - private ShapeBinding CreateDelegate( + private Func CreateDelegate( ShapeAttributeOccurrence attributeOccurrence, ShapeDescriptor descriptor) { return context => { @@ -46,11 +47,15 @@ namespace Orchard.DisplayManagement.Descriptors.ShapeAttributeStrategy { } - private object PerformInvoke(DisplayContext displayContext, MethodInfo methodInfo, object serviceInstance) { + private IHtmlString PerformInvoke(DisplayContext displayContext, MethodInfo methodInfo, object serviceInstance) { var arguments = methodInfo.GetParameters() .Select(parameter => BindParameter(displayContext, parameter)); - return methodInfo.Invoke(serviceInstance, arguments.ToArray()); + return CoerceHtmlString(methodInfo.Invoke(serviceInstance, arguments.ToArray())); + } + + private static IHtmlString CoerceHtmlString(object invoke) { + return invoke as IHtmlString ?? (invoke != null ? new HtmlString(invoke.ToString()) : null); } private object BindParameter(DisplayContext displayContext, ParameterInfo parameter) { diff --git a/src/Orchard/DisplayManagement/Descriptors/ShapeTemplateStrategy/ShapeTemplateBindingStrategy.cs b/src/Orchard/DisplayManagement/Descriptors/ShapeTemplateStrategy/ShapeTemplateBindingStrategy.cs index 41f49eb94..900b0ecf8 100644 --- a/src/Orchard/DisplayManagement/Descriptors/ShapeTemplateStrategy/ShapeTemplateBindingStrategy.cs +++ b/src/Orchard/DisplayManagement/Descriptors/ShapeTemplateStrategy/ShapeTemplateBindingStrategy.cs @@ -3,6 +3,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; using Orchard.DisplayManagement.Implementation; @@ -79,7 +80,7 @@ namespace Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy { } } - private object Render(ShapeDescriptor shapeDescriptor, DisplayContext displayContext, HarvestShapeInfo harvestShapeInfo, HarvestShapeHit harvestShapeHit) { + private IHtmlString Render(ShapeDescriptor shapeDescriptor, DisplayContext displayContext, HarvestShapeInfo harvestShapeInfo, HarvestShapeHit harvestShapeHit) { var htmlHelper = new HtmlHelper(displayContext.ViewContext, displayContext.ViewDataContainer); //return htmlHelper.Partial(harvestShapeInfo.TemplateVirtualPath, displayContext.Value); return htmlHelper.Partial(harvestShapeInfo.TemplateVirtualPath.Replace("\\", "/") + ".cshtml", displayContext.Value);