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-documentsession-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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With