This project is read-only.

IoC Resolve methods

Jun 17, 2010 at 6:55 PM

Is it possible to setup resolve method ? 

etc. I,d like to register IDocumentSession to be resolved PerThread or as Singleton or to as Transistent.

Jun 17, 2010 at 7:24 PM

I think I have foun solution for that.

 

public class AppModule : NinjectModule  

 {      

 private const string RavenSessionKey = "RavenMVC.Session";      

 private static DocumentStore documentStore;


public override void Load()      

 {          

 Bind<IDocumentSession>().ToConstant(documentStore.OpenSession()).InRequestScope();    

  }

        private void InitRavenDb()      

 {          

     documentStore = new DocumentStore { Url = "http://localhost:8080/" };      

     documentStore.Initialize();      

     documentStore.Conventions.IdentityPartsSeparator = "-";      

     documentStore.DatabaseCommands.PutIndex<Post, Post>(PostIndex.ByTitle, PostIndex.ByTitleIndexDefinition);    

   }  

 }


public partial class MvcApplication : TurbineApplication    

{    

static MvcApplication()      

{        

 IKernel kernel = new StandardKernel(new AppModule());    

       ServiceLocatorManager.SetLocatorProvider(() => new NinjectServiceLocator(kernel));  

}

...

}

 

I hope it works!

 

 

 

Coordinator
Jun 17, 2010 at 7:27 PM

Yes, it is but not through the IServiceLocator interface. Each of the ServiceLocator implementations (Ninject, SM, Windsor, etc.) that ship with Turbine allow you to pass in your own pre-initialized container for the system to use. This was done in order to support these type of scenarios. For example, for you IDocumentSession, you can do the following (the code below assumes you're using Windsor but it can be similar for other IoC containers):

static IWindsorContainer CreateWindsorContainer() {
    // Create the empty container to use
    WindsorContainer container = new WindsorContainer();

   // Register fluently
   // For more info, go to http://using.castleproject.org/display/IoC/Fluent+Registration+API
   container.Register(Component.For<IDocumentSession>()
                   .ImplementedBy<MyDocumentSession>()
                   .LifeStyle
                   .Singleton));
    
   return container;
}

From here you can pass the result from this method into your container, like so:

static DefaultMvcApplication() {
   // Get the default container to use with the application
   var defaultContainer = CreateWindsorContainer();
   
   // Pass in the initiated container to the service locator to use
   ServiceLocatorManager.SetLocatorProvider(() => new WindsorServiceLocator(defaultContainer));
}

A while back I blogged as why I took this approach with MVC Turbine, the Injecting your Favorite IoC into MVC Turbine.

Does this help out?

 

Coordinator
Jun 17, 2010 at 7:29 PM

Yep, that's it! You just beat me to the punch! :)

Again this was done in order not to hinder the power of your IoC of choice and the services it provides to your application. Turbine is just the integration between IoC and MVC with composition support.

Thanks for checking out MVC Turbine!

Jun 17, 2010 at 7:45 PM

Some times I have to ask a question, and then a solution comes into my mind :) Thanks for your reply!