Skip to content

Instantly share code, notes, and snippets.

@synhershko
Last active December 10, 2015 02:09

Revisions

  1. synhershko revised this gist Jan 14, 2013. 2 changed files with 19 additions and 6 deletions.
    6 changes: 3 additions & 3 deletions CustomBootstraper.cs
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@ protected override NancyInternalConfiguration InternalConfiguration
    {
    get
    {
    return NancyInternalConfiguration
    .WithOverrides(x => x.ViewLocationProvider = typeof (RavenViewLocationProvider))
    .WithIgnoredAssembly(asm => asm.FullName.StartsWith("RavenDB", StringComparison.InvariantCulture)); // or override ConfigureApplicationContainer to set AutoRegister to false
    return NancyInternalConfiguration
    .WithOverrides(x => x.ViewLocationProvider = typeof (RavenViewLocationProvider))
    .WithIgnoredAssembly(asm => asm.FullName.StartsWith("RavenDB", StringComparison.InvariantCulture)); // or override ConfigureApplicationContainer to set AutoRegister to false
    }
    }
    19 changes: 16 additions & 3 deletions RavenViewLocationProvider.cs
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@

    namespace NSemble.Core.Nancy
    {
    public class RavenViewLocationProvider : IViewLocationProvider
    public class RavenViewLocationProvider : IViewLocationProvider
    {
    private readonly IViewLocationProvider defaultViewLocationProvider;

    @@ -24,18 +24,31 @@ public RavenViewLocationProvider(IRootPathProvider rootPathProvider, IFileSystem

    public IEnumerable<ViewLocationResult> GetLocatedViews(IEnumerable<string> supportedViewExtensions)
    {
    var sb = new StringBuilder();

    // Make sure to only load saved views with supported extensions
    foreach (var s in supportedViewExtensions)
    {
    if (sb.Length > 0)
    sb.Append("|");
    sb.Append("*.");
    sb.Append(s);
    }

    ViewTemplate[] views = null;
    using (var session = NSembleModule.DocumentStore.OpenSession())
    {
    views = session.Advanced.LoadStartingWith<ViewTemplate>(Constants.RavenViewDocumentPrefix, null, 0, 1024);
    // It's probably safe to assume we will have no more than 1024 views, so no reason to bother with paging
    views = session.Advanced.LoadStartingWith<ViewTemplate>(Constants.RavenViewDocumentPrefix, sb.ToString(), 0, 1024);
    }

    // Read the views from the default location
    IEnumerable<ViewLocationResult> defaultViews = defaultViewLocationProvider.GetLocatedViews(supportedViewExtensions);
    if (views.Length == 0)
    return defaultViews;

    var ret = new HashSet<ViewLocationResult>(from v in views
    where supportedViewExtensions.Contains(v.Extension)
    where supportedViewExtensions.Contains(v.Extension)
    select new ViewLocationResult(
    v.Location,
    v.Name,
  2. synhershko created this gist Dec 23, 2012.
    9 changes: 9 additions & 0 deletions CustomBootstraper.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    protected override NancyInternalConfiguration InternalConfiguration
    {
    get
    {
    return NancyInternalConfiguration
    .WithOverrides(x => x.ViewLocationProvider = typeof (RavenViewLocationProvider))
    .WithIgnoredAssembly(asm => asm.FullName.StartsWith("RavenDB", StringComparison.InvariantCulture)); // or override ConfigureApplicationContainer to set AutoRegister to false
    }
    }
    51 changes: 51 additions & 0 deletions RavenViewLocationProvider.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using NSemble.Core.Models;
    using Nancy;
    using Nancy.ViewEngines;

    namespace NSemble.Core.Nancy
    {
    public class RavenViewLocationProvider : IViewLocationProvider
    {
    private readonly IViewLocationProvider defaultViewLocationProvider;

    public RavenViewLocationProvider(IRootPathProvider rootPathProvider)
    {
    defaultViewLocationProvider = new FileSystemViewLocationProvider(rootPathProvider);
    }

    public RavenViewLocationProvider(IRootPathProvider rootPathProvider, IFileSystemReader fileSystemReader)
    {
    defaultViewLocationProvider = new FileSystemViewLocationProvider(rootPathProvider, fileSystemReader);
    }

    public IEnumerable<ViewLocationResult> GetLocatedViews(IEnumerable<string> supportedViewExtensions)
    {
    ViewTemplate[] views = null;
    using (var session = NSembleModule.DocumentStore.OpenSession())
    {
    views = session.Advanced.LoadStartingWith<ViewTemplate>(Constants.RavenViewDocumentPrefix, null, 0, 1024);
    }

    IEnumerable<ViewLocationResult> defaultViews = defaultViewLocationProvider.GetLocatedViews(supportedViewExtensions);
    if (views.Length == 0)
    return defaultViews;

    var ret = new HashSet<ViewLocationResult>(from v in views
    where supportedViewExtensions.Contains(v.Extension)
    select new ViewLocationResult(
    v.Location,
    v.Name,
    v.Extension,
    () => new StringReader(v.Contents)));

    foreach (var v in defaultViews)
    ret.Add(v);

    return ret;
    }
    }
    }