Skip to content

Instantly share code, notes, and snippets.

@miguelludert
Last active December 15, 2015 13:49
Show Gist options
  • Save miguelludert/5270504 to your computer and use it in GitHub Desktop.
Save miguelludert/5270504 to your computer and use it in GitHub Desktop.
RavenDB helper class for creating singleton document stores and embedded stores for unit testing.
private Lazy<IDocumentSession> LazyDocumentSession = RavenHelper.CreateLazySession();
public IDocumentSession DocumentSession { get { return LazyDocumentSession.Value; } }
protected override void Dispose(bool disposing)
{
if (LazyDocumentSession.IsValueCreated)
LazyDocumentSession.Value.Dispose();
base.Dispose(disposing);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Client;
using Raven.Client.Document;
using System.ComponentModel.DataAnnotations;
using Raven.Client.Indexes;
using System.Reflection;
namespace RavenDB
{
public class RavenHelper
{
public static Lazy<IDocumentSession> CreateLazySession() { return new Lazy<IDocumentSession>(() => Store.OpenSession()); }
public static IDocumentSession OpenSession() { return Store.OpenSession(); }
public static IDocumentStore Store { get; set; }
public const string IdentityPartsSeparator = "/";
public static Func<Type, string> FindTypeTagName = (t) => t.Name;
public static Func<PropertyInfo,bool> FindIdentityProperty = (pi) => string.Compare(pi.Name, "ID", true) == 0;
public static void InitStore()
{
if (Store == null)
{
Store = new DocumentStore()
{
Url = "http://localhost:8080/",
DefaultDatabase = "MyDatabase",
};
}
Store.Conventions.IdentityPartsSeparator = IdentityPartsSeparator;
Store.Conventions.FindTypeTagName = FindTypeTagName;
Store.Conventions.FindIdentityProperty = FindIdentityProperty;
Store.Initialize();
IndexCreation.CreateIndexes(typeof(RavenHelper).Assembly, Store);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Raven.Client.Embedded;
namespace RavenDB
{
public class RavenTestingHelper : IDisposable
{
public static RavenTestingHelper InitTestingStore()
{
if (!(RavenDB.RavenHelper.Store is EmbeddableDocumentStore))
{
RavenDB.RavenHelper.Store = new EmbeddableDocumentStore()
{
RunInMemory = true
};
RavenDB.RavenHelper.InitStore();
}
return new RavenTestingHelper();
}
private RavenTestingHelper()
{
}
public void Dispose()
{
RavenDB.RavenHelper.Store.Dispose();
RavenDB.RavenHelper.Store = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment