开发者

Need help getting Ninject equivalent for StructureMap syntax

I am trying to implement IoC (Ninject) for Ravendb and have ran into a little snag. I am using code from http://www.dotnetguy.co.uk/post/2010/06/12/raven-db-ndash-part-1-ndash-docume开发者_如何学编程ntsession-per-request-with-structuremap to help.

public interface IRavenSessionFactoryBuilder
{
    IRavenSessionFactory GetSessionFactory();
}

public class RavenSessionFactoryBuilder : IRavenSessionFactoryBuilder
{
    private IRavenSessionFactory _ravenSessionFactory;

    public IRavenSessionFactory GetSessionFactory()
    {
        return _ravenSessionFactory ?? (_ravenSessionFactory = CreateSessionFactory());
    }

    private static IRavenSessionFactory CreateSessionFactory()
    {
        Debug.Write("IRavenSessionFactory Created");
        return new RavenSessionFactory(new DocumentStore
                                           {
                                               Url =
                                                   System.Web.Configuration.WebConfigurationManager.AppSettings[
                                                       "Raven.DocumentStore"]
                                           });
    }
}

public interface IRavenSessionFactory
{
    IDocumentSession CreateSession();
}

public class RavenSessionFactory : IRavenSessionFactory
{
    private readonly IDocumentStore _documentStore;

    public RavenSessionFactory(IDocumentStore documentStore)
    {
        if (_documentStore != null) return;
        _documentStore = documentStore;
        _documentStore.Initialize();
    }

    public IDocumentSession CreateSession()
    {
        Debug.Write("IDocumentSession Created");
        return _documentStore.OpenSession();
    }
}

I am unsure how to convert the following structure map syntax.

ObjectFactory.Configure(x => x.For<IDocumentSession>()
                  .HybridHttpOrThreadLocalScoped()
                  .AddInstances(inst => inst.ConstructedBy
                    (context => context.GetInstance<IRavenSessionFactoryBuilder>()
                      .GetSessionFactory().CreateSession())));

In My attempt, _ravenSessionFactory is null on every request because of the new constructor.

Bind<IDocumentSession>().ToMethod(
            x => new RavenSessionFactoryBuilder().GetSessionFactory().CreateSession()).RequestScope();

Thanks for anyone taking the time to try and help explain.


Factories are called providers in Ninject. Convert the SessionFactory to a SessionProvider:-

public class RavenSessionProvider : Provider<IDocumentSession>
{
    private readonly IDocumentStore _documentStore;

    public RavenSessionFactory(IDocumentStore documentStore)
    {
        _documentStore = documentStore;
    }

    public IDocumentSession GetInstance(IContext ctx)
    {
        Debug.Write("IDocumentSession Created");
        return _documentStore.OpenSession();
    }
}

Also change your RavenSessionFactoryBuilder to a DocumentStoreProvider:-

public class DocumentStoreProvider : Provider<IDocumentStore>
{
    public IDocumentStore GetInstance(IContext ctx)
    {
        var store = new DocumentStore 
                   { Url = System.Web.Configuration.WebConfigurationManager.AppSettings["Raven.DocumentStore"]});
        store.Initialize();
        return store;
    }
}

And add bindings:

Bind<RavenSessionProvider>().ToSelf().InSingletonScope()
Bind<IDocumentSession>().ToProvider<RavenSessionProvider>();


Instead of new RavenSessionFactoryBuilder().GetSessionFactory()...., I would think you'd want:

Kernel.Get<IRavenSessionFactoryBuilder>().GetSessionFactory()....

where you've done something like this beforehand:

Bind<IRavenSessionFactoryBuilder>().To<IRavenSessionFactoryBuilder>()
  .InSingletonScope();

Disclaimer: I've never tried a Get in a Bind statement before. You might need a factory method.


Ninject basically has 5 options for scope.

TransientScope - the one you're using means that a new instance is created for every request

SingletonScope - only one instance is ever created

ThreadScope - only one instance is created per thread

RequestScope - only one instance is created per HttpRequest

Custom - you provide the scope object

If you're creating a web app you can just specify .InRequestScope() If it's a windows app you might specify .InThreadScope()

Finally if you must specify a hybrid (I'm not totally sure how it works in structure map) you might do .InScope(ctx => HttpRequest.Current != null ? HttpRequest.Current : Thread.CurrentThread)


You don't need to create a factory or provider etc to do this.

Ninject does the session per request work for you create a Ninject module that binds a document store in InSingletonScope(), then bind a DocumentSession in Request scope and your ready to go.

I've blogged a step by step guide for Ninject and RavenDB

http://www.dalsoft.co.uk/blog/index.php/2012/04/12/mvc-get-ravendb-up-and-running-in-5-minutes-using-ninject/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜