From b82495f138eed7a88853af8d50495cdecb9a6059 Mon Sep 17 00:00:00 2001 From: Andreas Hakansson Date: Tue, 10 Apr 2012 21:22:12 +0200 Subject: [PATCH 1/5] Converted fields to public properties Made sure that the HtmlHelpers and UrlHelpers types now exposes a richer set of information. This will enable more powerful extension methods to be written by the API consumers. --- src/Nancy.ViewEngines.Razor/HtmlHelpers.cs | 44 +++++++++++++++++----- src/Nancy.ViewEngines.Razor/UrlHelpers.cs | 21 ++++++++--- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs b/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs index d5a8e190e9..85b0ecb22e 100644 --- a/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs +++ b/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs @@ -3,16 +3,21 @@ using System; using System.IO; + public static class HtmlHelpersExtensions + { + public static IHtmlString GetUrl(this IHtmlHelpers helpers) + { + //return new NonEncodedHtmlString(helpers.RenderContext.Context.Request.Url.ToString()); + return new NonEncodedHtmlString("Hello"); + } + } + /// /// Helpers to generate html content. /// /// The type of the model. public class HtmlHelpers : IHtmlHelpers { - private readonly TModel model; - private readonly RazorViewEngine engine; - private readonly IRenderContext renderContext; - /// /// Initializes a new instance of the class. /// @@ -21,11 +26,29 @@ public class HtmlHelpers : IHtmlHelpers /// The model that is used by the page where the helpers are invoked. public HtmlHelpers(RazorViewEngine engine, IRenderContext renderContext, TModel model) { - this.engine = engine; - this.renderContext = renderContext; - this.model = model; + this.Engine = engine; + this.RenderContext = renderContext; + this.Model = model; } + /// + /// + /// + /// + public TModel Model { get; set; } + + /// + /// + /// + /// + public RazorViewEngine Engine { get; set; } + + /// + /// + /// + /// + public IRenderContext RenderContext { get; set; } + /// /// Renders a partial with the given view name. /// @@ -44,9 +67,9 @@ public IHtmlString Partial(string viewName) /// An representation of the partial. public IHtmlString Partial(string viewName, dynamic modelForPartial) { - var view = this.renderContext.LocateView(viewName, modelForPartial); + var view = this.RenderContext.LocateView(viewName, modelForPartial); - var response = this.engine.RenderView(view, modelForPartial, this.renderContext); + var response = this.Engine.RenderView(view, modelForPartial, this.RenderContext); Action action = response.Contents; var mem = new MemoryStream(); @@ -74,7 +97,8 @@ public IHtmlString Raw(string text) /// An representation of the anti forgery token. public IHtmlString AntiForgeryToken() { - var tokenKeyValue = this.renderContext.GetCsrfToken(); + var tokenKeyValue = + this.RenderContext.GetCsrfToken(); return new NonEncodedHtmlString(String.Format("", tokenKeyValue.Key, tokenKeyValue.Value)); } diff --git a/src/Nancy.ViewEngines.Razor/UrlHelpers.cs b/src/Nancy.ViewEngines.Razor/UrlHelpers.cs index 5212985457..a9a460b09a 100644 --- a/src/Nancy.ViewEngines.Razor/UrlHelpers.cs +++ b/src/Nancy.ViewEngines.Razor/UrlHelpers.cs @@ -6,9 +6,6 @@ namespace Nancy.ViewEngines.Razor /// The type of the model. public class UrlHelpers : IUrlHelpers { - private readonly RazorViewEngine razorViewEngine; - private readonly IRenderContext renderContext; - /// /// Initializes a new instance of the class. /// @@ -16,17 +13,29 @@ public class UrlHelpers : IUrlHelpers /// The render context. public UrlHelpers(RazorViewEngine razorViewEngine, IRenderContext renderContext) { - this.razorViewEngine = razorViewEngine; - this.renderContext = renderContext; + this.RazorViewEngine = razorViewEngine; + this.RenderContext = renderContext; } + /// + /// + /// + /// + public RazorViewEngine RazorViewEngine { get; set; } + + /// + /// + /// + /// + public IRenderContext RenderContext { get; set; } + /// /// Retrieves the absolute url of the specified path. /// /// The path. public string Content(string path) { - return renderContext.ParsePath(path); + return this.RenderContext.ParsePath(path); } } } \ No newline at end of file From 0620633eb15ab39446df6058ca7ecc201a5631e7 Mon Sep 17 00:00:00 2001 From: Andreas Hakansson Date: Tue, 10 Apr 2012 22:14:30 +0200 Subject: [PATCH 2/5] Removed IHtmlHelpers and IUrlHelpers The interfaces we pointless. Swapping out the implementations, that are used by the NancyRazorViewBase type, would mean rewriting parts of the RazorEngine and that's not something a consumer of Nancy would be doing. --- src/Nancy.ViewEngines.Razor/HtmlHelpers.cs | 14 ++----- src/Nancy.ViewEngines.Razor/IHtmlHelpers.cs | 37 ------------------- src/Nancy.ViewEngines.Razor/IUrlHelpers.cs | 11 ------ .../Nancy.ViewEngines.Razor.csproj | 2 - .../NancyRazorViewBase.cs | 5 +-- src/Nancy.ViewEngines.Razor/UrlHelpers.cs | 2 +- 6 files changed, 6 insertions(+), 65 deletions(-) delete mode 100644 src/Nancy.ViewEngines.Razor/IHtmlHelpers.cs delete mode 100644 src/Nancy.ViewEngines.Razor/IUrlHelpers.cs diff --git a/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs b/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs index 85b0ecb22e..bd29d52e96 100644 --- a/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs +++ b/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs @@ -2,24 +2,16 @@ { using System; using System.IO; - - public static class HtmlHelpersExtensions - { - public static IHtmlString GetUrl(this IHtmlHelpers helpers) - { - //return new NonEncodedHtmlString(helpers.RenderContext.Context.Request.Url.ToString()); - return new NonEncodedHtmlString("Hello"); - } - } + using System.Linq.Expressions; /// /// Helpers to generate html content. /// /// The type of the model. - public class HtmlHelpers : IHtmlHelpers + public class HtmlHelpers { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The razor view engine instance that the helpers are being used by. /// The that the helper are being used by. diff --git a/src/Nancy.ViewEngines.Razor/IHtmlHelpers.cs b/src/Nancy.ViewEngines.Razor/IHtmlHelpers.cs deleted file mode 100644 index 77ea2b75a7..0000000000 --- a/src/Nancy.ViewEngines.Razor/IHtmlHelpers.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace Nancy.ViewEngines.Razor -{ - /// - /// Defines the functionality of a html helper. - /// - /// The type of the model. - public interface IHtmlHelpers - { - /// - /// Renders a partial with the given view name. - /// - /// Name of the view. - /// An representation of the partial. - IHtmlString Partial(string viewName); - - /// - /// Renders a partial with the given view name. - /// - /// Name of the view. - /// The model. - /// An representation of the partial. - IHtmlString Partial(string viewName, dynamic model); - - /// - /// Returns an html string composed of raw, non-encoded text. - /// - /// The text. - /// An representation of the raw text. - IHtmlString Raw(string text); - - /// - /// Creates an anti-forgery token. - /// - /// An representation of the anti forgery token. - IHtmlString AntiForgeryToken(); - } -} \ No newline at end of file diff --git a/src/Nancy.ViewEngines.Razor/IUrlHelpers.cs b/src/Nancy.ViewEngines.Razor/IUrlHelpers.cs deleted file mode 100644 index 697edda640..0000000000 --- a/src/Nancy.ViewEngines.Razor/IUrlHelpers.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Nancy.ViewEngines.Razor -{ - public interface IUrlHelpers - { - /// - /// Retrieves the absolute url of the specified path. - /// - /// The path. - string Content(string path); - } -} \ No newline at end of file diff --git a/src/Nancy.ViewEngines.Razor/Nancy.ViewEngines.Razor.csproj b/src/Nancy.ViewEngines.Razor/Nancy.ViewEngines.Razor.csproj index 19bc1b9b28..8f31d280a9 100644 --- a/src/Nancy.ViewEngines.Razor/Nancy.ViewEngines.Razor.csproj +++ b/src/Nancy.ViewEngines.Razor/Nancy.ViewEngines.Razor.csproj @@ -94,11 +94,9 @@ - - diff --git a/src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs b/src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs index 369215f6ab..74925d15bd 100644 --- a/src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs +++ b/src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Globalization; using System.Text; - using Nancy.Helpers; /// @@ -225,7 +224,7 @@ public abstract class NancyRazorViewBase : NancyRazorViewBase /// /// Gets the Html helper. /// - public IHtmlHelpers Html { get; private set; } + public HtmlHelpers Html { get; private set; } /// /// Gets the model. @@ -235,7 +234,7 @@ public abstract class NancyRazorViewBase : NancyRazorViewBase /// /// Gets the Url helper. /// - public IUrlHelpers Url { get; private set; } + public UrlHelpers Url { get; private set; } /// /// Non-model specific data for rendering in the response diff --git a/src/Nancy.ViewEngines.Razor/UrlHelpers.cs b/src/Nancy.ViewEngines.Razor/UrlHelpers.cs index a9a460b09a..7f5216ebc1 100644 --- a/src/Nancy.ViewEngines.Razor/UrlHelpers.cs +++ b/src/Nancy.ViewEngines.Razor/UrlHelpers.cs @@ -4,7 +4,7 @@ namespace Nancy.ViewEngines.Razor /// Helpers for url related functions. /// /// The type of the model. - public class UrlHelpers : IUrlHelpers + public class UrlHelpers { /// /// Initializes a new instance of the class. From 614e87892e5d2364a998dbbc7f723f6b8b6dc1b7 Mon Sep 17 00:00:00 2001 From: Andreas Hakansson Date: Tue, 10 Apr 2012 22:18:22 +0200 Subject: [PATCH 3/5] Added missing XML comments --- src/Nancy.ViewEngines.Razor/HtmlHelpers.cs | 12 ++++++------ src/Nancy.ViewEngines.Razor/UrlHelpers.cs | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs b/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs index bd29d52e96..5892d1fc71 100644 --- a/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs +++ b/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs @@ -24,21 +24,21 @@ public HtmlHelpers(RazorViewEngine engine, IRenderContext renderContext, TModel } /// - /// + /// The model that is being used by the current view. /// - /// + /// An instance of the view model. public TModel Model { get; set; } /// - /// + /// The engine that is currently rendering the view. /// - /// + /// A instance. public RazorViewEngine Engine { get; set; } /// - /// + /// The context of the current render operation. /// - /// + /// An intance. public IRenderContext RenderContext { get; set; } /// diff --git a/src/Nancy.ViewEngines.Razor/UrlHelpers.cs b/src/Nancy.ViewEngines.Razor/UrlHelpers.cs index 7f5216ebc1..5db11d7dec 100644 --- a/src/Nancy.ViewEngines.Razor/UrlHelpers.cs +++ b/src/Nancy.ViewEngines.Razor/UrlHelpers.cs @@ -18,15 +18,15 @@ public UrlHelpers(RazorViewEngine razorViewEngine, IRenderContext renderContext) } /// - /// + /// The engine that is currently rendering the view. /// - /// + /// A instance. public RazorViewEngine RazorViewEngine { get; set; } /// - /// + /// The context of the current render operation. /// - /// + /// An intance. public IRenderContext RenderContext { get; set; } /// From c4dee16fc53c574c6c555282f3c9a6935586d50b Mon Sep 17 00:00:00 2001 From: Andreas Hakansson Date: Tue, 10 Apr 2012 22:21:25 +0200 Subject: [PATCH 4/5] Ol' style cop OCD acting up again --- src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs b/src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs index 74925d15bd..e948813af7 100644 --- a/src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs +++ b/src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs @@ -44,10 +44,7 @@ public abstract class NancyRazorViewBase /// public bool HasLayout { - get - { - return !String.IsNullOrEmpty(this.Layout); - } + get { return !String.IsNullOrEmpty(this.Layout); } } /// From e87869575938a3348837764ffd713387854a259c Mon Sep 17 00:00:00 2001 From: Andreas Hakansson Date: Tue, 10 Apr 2012 22:50:46 +0200 Subject: [PATCH 5/5] Made NonEncodedHtmlString implicitly cast from string --- src/Nancy.ViewEngines.Razor/NonEncodedHtmlString.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Nancy.ViewEngines.Razor/NonEncodedHtmlString.cs b/src/Nancy.ViewEngines.Razor/NonEncodedHtmlString.cs index bd086b601f..d0ed49420b 100644 --- a/src/Nancy.ViewEngines.Razor/NonEncodedHtmlString.cs +++ b/src/Nancy.ViewEngines.Razor/NonEncodedHtmlString.cs @@ -24,5 +24,10 @@ public string ToHtmlString() { return value; } + + public static implicit operator NonEncodedHtmlString(string value) + { + return new NonEncodedHtmlString(value); + } } } \ No newline at end of file