Ehsan Ghanbari

Experience, DotNet, Solutions

Register the store and session of RavenDb with Autofac

The IDocumentSession in RavenDb should be registered per lifetime scope while the DocumentStore should be resisted as a single instance in Autofac. It's my third time that I'm making a big mistake about the registering of dependencies in autofac! Although it's not a production code and it was just a sort of practice for me.

 

class DependencyRegistery
    {
        public static void Register()
        {
            var builder = new ContainerBuilder();

            builder.Register(x =>
            {
                var store = new DocumentStore();
                store.Initialize();
                return store;
            }).As<IDocumentStore>().SingleInstance();


            builder.Register(x => x.Resolve<IDocumentStore>().OpenSession())
                 .As<IDocumentSession>()
                 .InstancePerLifetimeScope();
                
        }
    }

 



About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
using Log4net in asp.net MVC4
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
What's the DDD-lite?
Multiple submit buttons in asp.net MVC