using RavenDB with ServiceStack

前端 未结 4 1065
梦毁少年i
梦毁少年i 2021-02-06 13:14

I read this post by Phillip Haydon about how to use NHibernate/RavenDB with ServiceStack.
I don\'t see the point about getting the IDocumentStore and open new session every

4条回答
  •  礼貌的吻别
    2021-02-06 13:38

    I am using funq with RequestScope for my RavenSession, and now i update it to:

    public class RavenSession : IRavenSession, IDisposable
    {
        #region Data Members
    
        private readonly IDocumentStore _store;
        private readonly IRequestContext _context;
        private IDocumentSession _innerSession;
    
        #endregion
    
        #region Properties
    
        public IDocumentSession InnerSession
        {
            get { return _innerSession ?? (_innerSession = _store.OpenSession()); }
        }
    
        #endregion
    
        #region Ctor
    
        public RavenSession(IDocumentStore store, IRequestContext context)
        {
            _store = store;
            _context = context;
        }
    
        #endregion
    
        #region IDocumentSession Delegation
    
        public ISyncAdvancedSessionOperation Advanced
        {
            get { return InnerSession.Advanced; }
        }
    
        public void Delete(T entity)
        {
            InnerSession.Delete(entity);
        }
    
        public ILoaderWithInclude Include(string path)
        {
            return InnerSession.Include(path);
        }
    
        public ILoaderWithInclude Include(Expression> path)
        {
            return InnerSession.Include(path);
        }
    
        public ILoaderWithInclude Include(Expression> path)
        {
            return InnerSession.Include(path);
        }
    
        public T Load(string id)
        {
            return InnerSession.Load(id);
        }
    
        public T[] Load(params string[] ids)
        {
            return InnerSession.Load(ids);
        }
    
        public T Load(ValueType id)
        {
            return InnerSession.Load(id);
        }
    
        public T[] Load(IEnumerable ids)
        {
            return InnerSession.Load(ids);
        }
    
        public IRavenQueryable Query() where TIndexCreator : AbstractIndexCreationTask, new()
        {
            return InnerSession.Query();
        }
    
        public IRavenQueryable Query()
        {
            return InnerSession.Query();
        }
    
        public IRavenQueryable Query(string indexName)
        {
            return InnerSession.Query(indexName);
        }
    
        public void Store(dynamic entity, string id)
        {
            InnerSession.Store(entity, id);
        }
    
        public void Store(object entity, Guid etag, string id)
        {
            InnerSession.Store(entity, etag, id);
        }
    
        public void Store(object entity, Guid etag)
        {
            InnerSession.Store(entity, etag);
        }
    
        public void Store(dynamic entity)
        {
            InnerSession.Store(entity);
        }
    
        #endregion
    
        #region Implementation of IDisposable
    
        public void Dispose()
        {
            if (_innerSession != null)
            {
                var httpResponse = _context.Get();
    
                try
                {
                    if (!httpResponse.IsErrorResponse())
                    {
                        _innerSession.SaveChanges();
                    }
                }
                finally
                {
                    _innerSession.Dispose();
                }
            }
        }
    
        #endregion
    }
    
    
    

    but this would not work because:
    1) although i am using RequestScope, no one is register the IRequestContext of the request so funq cant resolve my RavenSession.
    2) funq does not run the Dispose method after the request is done, which is odd.

    提交回复
    热议问题