Last active
December 10, 2015 02:09
-
-
Save synhershko/4365976 to your computer and use it in GitHub Desktop.
Custom NancyFX IViewLocationProvider implementation to allow for loading views from a RavenDB server instance. Views loaded from RavenDB will override views which exist on the File System.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
{ | |
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()) | |
{ | |
// 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) | |
select new ViewLocationResult( | |
v.Location, | |
v.Name, | |
v.Extension, | |
() => new StringReader(v.Contents))); | |
foreach (var v in defaultViews) | |
ret.Add(v); | |
return ret; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment