Skip to content

Instantly share code, notes, and snippets.

@PureKrome
Forked from ronnieoverby/gist:2372753
Created April 13, 2012 01:52

Revisions

  1. PureKrome revised this gist Apr 13, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ public RavenContext(RavenConnectionInfo connInfo)
    // New Lazy Loaded session.
    private IDocumentSession Session
    {
    get { return _session ?? _docStore.OpenSession(); }
    get { return _session ?? (_session = _docStore.OpenSession()); }
    }


  2. PureKrome revised this gist Apr 13, 2012. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    namespace RavenLinqpadDriver
    {
    public class RavenContext : IDocumentSession
    @@ -30,17 +29,17 @@ private IDocumentSession Session

    public IRavenQueryable<T> Query<T, TIndexCreator>() where TIndexCreator : AbstractIndexCreationTask, new()
    {
    return _session.Query<T, TIndexCreator>();
    return Session.Query<T, TIndexCreator>();
    }

    public IRavenQueryable<T> Query<T>()
    {
    return _session.Query<T>();
    return Session.Query<T>();
    }

    public IRavenQueryable<T> Query<T>(string indexName)
    {
    return _session.Query<T>(indexName);
    return Session.Query<T>(indexName);
    }

    }
  3. PureKrome revised this gist Apr 13, 2012. 1 changed file with 6 additions and 147 deletions.
    153 changes: 6 additions & 147 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,3 @@
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Linq.Expressions;
    using Fasterflect;
    using Raven.Client;
    using Raven.Client.Connection.Profiling;
    using Raven.Client.Document;
    using Raven.Client.Indexes;
    using Raven.Client.Linq;
    using RavenLinqpadDriver.Bridge;

    namespace RavenLinqpadDriver
    {
    @@ -26,110 +14,19 @@ public RavenContext(RavenConnectionInfo connInfo)

    InitDocStore(connInfo);
    SetupLogWriting();
    _session = _docStore.OpenSession();
    }

    private void SetupLogWriting()
    {
    _docStore.JsonRequestFactory.LogRequest += new EventHandler<RequestResultArgs>(LogRequest);
    // Don't do this....
    //_session = _docStore.OpenSession();
    }

    void LogRequest(object sender, RequestResultArgs e)
    // New Lazy Loaded session.
    private IDocumentSession Session
    {
    if (LogWriter == null) return;

    LogWriter.WriteLine(string.Format(@"
    {0} - {1}
    Url: {2}
    Duration: {3} milliseconds
    Method: {4}
    Posted Data: {5}
    Http Result: {6}
    Result Data: {7}
    ",
    e.At, // 0
    e.Status, // 1
    e.Url, // 2
    e.DurationMilliseconds, // 3
    e.Method, // 4
    e.PostedData, // 5
    e.HttpResult, // 6
    e.Result)); // 7
    get { return _session ?? _docStore.OpenSession(); }
    }

    private void InitDocStore(RavenConnectionInfo conn)
    {
    if (conn == null)
    throw new ArgumentNullException("conn", "conn is null.");

    _docStore = conn.CreateDocStore();

    // search for a user defined initializer
    var refAssemblyNames = this.GetType().Assembly.GetReferencedAssemblies();
    var assemblies = AppDomain.CurrentDomain.GetAssemblies();
    var initType = (from a in assemblies
    //let a = Assembly.Load(an)
    from t in a.TypesImplementing<IConfigureDocumentStore>()
    select t).FirstOrDefault();

    if (initType != null)
    {
    var docStoreInit = (IConfigureDocumentStore)initType.CreateInstance();
    docStoreInit.ConfigureDocumentStore(_docStore);
    }

    _docStore.Initialize();
    }

    public void Dispose()
    {
    if (_session != null)
    _session.Dispose();

    if (_docStore != null && !_docStore.WasDisposed)
    _docStore.Dispose();
    }

    #region IDocumentSession Members
    public ISyncAdvancedSessionOperation Advanced
    {
    get { return _session.Advanced; }
    }

    public void Delete<T>(T entity)
    {
    _session.Delete<T>(entity);
    }

    public ILoaderWithInclude<T> Include<T>(Expression<Func<T, object>> path)
    {
    return _session.Include<T>(path);
    }

    public ILoaderWithInclude<object> Include(string path)
    {
    return _session.Include(path);
    }

    public T Load<T>(ValueType id)
    {
    return _session.Load<T>(id);
    }

    public T[] Load<T>(IEnumerable<string> ids)
    {
    return _session.Load<T>(ids);
    }

    public T[] Load<T>(params string[] ids)
    {
    return _session.Load<T>(ids);
    }

    public T Load<T>(string id)
    {
    return _session.Load<T>(id);
    }
    // The queries...

    public IRavenQueryable<T> Query<T, TIndexCreator>() where TIndexCreator : AbstractIndexCreationTask, new()
    {
    @@ -146,43 +43,5 @@ public IRavenQueryable<T> Query<T>(string indexName)
    return _session.Query<T>(indexName);
    }

    public void SaveChanges()
    {
    _session.SaveChanges();
    }

    public void Store(object entity, Guid etag, string id)
    {
    _session.Store(entity, etag, id);
    }

    public void Store(object entity, Guid etag)
    {
    _session.Store(entity, etag);
    }

    #if !NET35
    public void Store(dynamic entity, string id)
    {
    _session.Store(entity, id);
    }

    public void Store(dynamic entity)
    {
    _session.Store(entity);
    }
    #else
    public void Store(object entity, string id)
    {
    _session.Store(entity, id);
    }

    public void Store(object entity)
    {
    _session.Store(entity);
    }
    #endif

    #endregion
    }
    }
  4. @ronnieoverby ronnieoverby created this gist Apr 13, 2012.
    188 changes: 188 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,188 @@
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Linq.Expressions;
    using Fasterflect;
    using Raven.Client;
    using Raven.Client.Connection.Profiling;
    using Raven.Client.Document;
    using Raven.Client.Indexes;
    using Raven.Client.Linq;
    using RavenLinqpadDriver.Bridge;

    namespace RavenLinqpadDriver
    {
    public class RavenContext : IDocumentSession
    {
    private IDocumentStore _docStore;
    private IDocumentSession _session;
    internal TextWriter LogWriter { get; set; }

    public RavenContext(RavenConnectionInfo connInfo)
    {
    if (connInfo == null)
    throw new ArgumentNullException("conn", "conn is null.");

    InitDocStore(connInfo);
    SetupLogWriting();
    _session = _docStore.OpenSession();
    }

    private void SetupLogWriting()
    {
    _docStore.JsonRequestFactory.LogRequest += new EventHandler<RequestResultArgs>(LogRequest);
    }

    void LogRequest(object sender, RequestResultArgs e)
    {
    if (LogWriter == null) return;

    LogWriter.WriteLine(string.Format(@"
    {0} - {1}
    Url: {2}
    Duration: {3} milliseconds
    Method: {4}
    Posted Data: {5}
    Http Result: {6}
    Result Data: {7}
    ",
    e.At, // 0
    e.Status, // 1
    e.Url, // 2
    e.DurationMilliseconds, // 3
    e.Method, // 4
    e.PostedData, // 5
    e.HttpResult, // 6
    e.Result)); // 7
    }

    private void InitDocStore(RavenConnectionInfo conn)
    {
    if (conn == null)
    throw new ArgumentNullException("conn", "conn is null.");

    _docStore = conn.CreateDocStore();

    // search for a user defined initializer
    var refAssemblyNames = this.GetType().Assembly.GetReferencedAssemblies();
    var assemblies = AppDomain.CurrentDomain.GetAssemblies();
    var initType = (from a in assemblies
    //let a = Assembly.Load(an)
    from t in a.TypesImplementing<IConfigureDocumentStore>()
    select t).FirstOrDefault();

    if (initType != null)
    {
    var docStoreInit = (IConfigureDocumentStore)initType.CreateInstance();
    docStoreInit.ConfigureDocumentStore(_docStore);
    }

    _docStore.Initialize();
    }

    public void Dispose()
    {
    if (_session != null)
    _session.Dispose();

    if (_docStore != null && !_docStore.WasDisposed)
    _docStore.Dispose();
    }

    #region IDocumentSession Members
    public ISyncAdvancedSessionOperation Advanced
    {
    get { return _session.Advanced; }
    }

    public void Delete<T>(T entity)
    {
    _session.Delete<T>(entity);
    }

    public ILoaderWithInclude<T> Include<T>(Expression<Func<T, object>> path)
    {
    return _session.Include<T>(path);
    }

    public ILoaderWithInclude<object> Include(string path)
    {
    return _session.Include(path);
    }

    public T Load<T>(ValueType id)
    {
    return _session.Load<T>(id);
    }

    public T[] Load<T>(IEnumerable<string> ids)
    {
    return _session.Load<T>(ids);
    }

    public T[] Load<T>(params string[] ids)
    {
    return _session.Load<T>(ids);
    }

    public T Load<T>(string id)
    {
    return _session.Load<T>(id);
    }

    public IRavenQueryable<T> Query<T, TIndexCreator>() where TIndexCreator : AbstractIndexCreationTask, new()
    {
    return _session.Query<T, TIndexCreator>();
    }

    public IRavenQueryable<T> Query<T>()
    {
    return _session.Query<T>();
    }

    public IRavenQueryable<T> Query<T>(string indexName)
    {
    return _session.Query<T>(indexName);
    }

    public void SaveChanges()
    {
    _session.SaveChanges();
    }

    public void Store(object entity, Guid etag, string id)
    {
    _session.Store(entity, etag, id);
    }

    public void Store(object entity, Guid etag)
    {
    _session.Store(entity, etag);
    }

    #if !NET35
    public void Store(dynamic entity, string id)
    {
    _session.Store(entity, id);
    }

    public void Store(dynamic entity)
    {
    _session.Store(entity);
    }
    #else
    public void Store(object entity, string id)
    {
    _session.Store(entity, id);
    }

    public void Store(object entity)
    {
    _session.Store(entity);
    }
    #endif

    #endregion
    }
    }