Skip to content

Instantly share code, notes, and snippets.

@abdalla
Created March 5, 2012 22:38
Show Gist options
  • Save abdalla/1981661 to your computer and use it in GitHub Desktop.
Save abdalla/1981661 to your computer and use it in GitHub Desktop.
RavenDB Repository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Msdev.EventSystem.Core.Data.Raven;
using Raven.Client.Document;
namespace Msdev.EventSystem.Ui.Controllers.Factory
{
public class CustomControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null) return base.GetControllerInstance(requestContext, requestContext.GetType());
var docStore = HttpContext.Current.Application["DocumentStore"] as DocumentStore;
var repository = new RavenRepository(docStore);
return Activator.CreateInstance(controllerType, repository) as IController;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Msdev.EventSystem.Core.Entities.Raven;
namespace Msdev.EventSystem.Core.Data.Raven
{
public interface IRavenRepository
{
T SingleOrDefault<T>(Func<T, bool> predicate) where T : IModel;
IEnumerable<T> All<T>() where T : IModel;
void Delete<T>(T item) where T : IModel;
void Add<T>(T item) where T : IModel;
void Save();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Msdev.EventSystem.Core.Entities.Raven;
using Raven.Client.Document;
using Raven.Client;
namespace Msdev.EventSystem.Core.Data.Raven
{
public class RavenRepository : IRavenRepository
{
private DocumentStore _store;
private IDocumentSession _session;
public RavenRepository(DocumentStore store)
{
_store = store;
_session = _store.OpenSession();
}
public T SingleOrDefault<T>(Func<T, bool> predicate) where T : IModel
{
return _session.Query<T>().SingleOrDefault(predicate);
}
public IEnumerable<T> All<T>() where T : IModel
{
return _session.Query<T>();
}
public void Add<T>(T item) where T : IModel
{
_session.Store(item);
}
public void Delete<T>(T item) where T : IModel
{
_session.Delete(item);
}
public void Save()
{
_session.SaveChanges();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment