Need help getting the Ninject equivalent for StructureMap syntax

I try to implement IoC (Ninject) for Ravendb and stumbled upon a little grasp. I am using the code 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 not sure how to convert the following structure structure syntax.

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

In my attempt, _ravenSessionFactory is null for each request due to a new constructor.

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

Thank you for having time to try to help explain.

+4
source share
4 answers

Instead of new RavenSessionFactoryBuilder().GetSessionFactory().... , I think you need:

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

where you did something like this in advance:

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

Disclaimer: I have never tried Get in a Bind clause before. You may need a factory method.

+1
source

Factories are called suppliers at Ninject. Convert SessionFactory to 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 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 the bindings:

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

Ninject basically has 5 scope options.

TransientScope - the one you use means that a new instance is created for each request

SingletonScope - only one instance is created

ThreadScope - only one instance per thread is created

RequestScope - only one instance is created for HttpRequest

Custom - you provide a scope object

If you are creating a web application, you can simply specify .InRequestScope() If it is a Windows application, you can specify .InThreadScope()

Finally, if you have to specify a hybrid (I'm not quite sure how it works in the map structure), you can do .InScope(ctx => HttpRequest.Current != null ? HttpRequest.Current : Thread.CurrentThread)

0
source

For this you do not need to create a factory or provider, etc.

Ninject does the job of the request session to create the Ninject module, which binds the document store in InSingletonScope (), and then binds the DocumentSession in Request area and your availability.

I wrote a walkthrough 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
source

Source: https://habr.com/ru/post/1346808/


All Articles