I just put together a short screencast (about 4min) showing you have to configure SisoDb with an IoC-container in ASP.Net MVC using “One session per HttpRequest”. For this demo I will use Ninject.
The screencast is hosted in the SisoDb channel at Vimeo.
Updated: Mike Paterson has a GitHub repository with code for the episode, found here:
https://github.com/devlife/Sandbox/tree/master/SisoDb
Summarized
After having installed the “Ninject.MVC3 NuGet package”, I added a NinjectModule named “DbConfig” under a new folder/namespace “IoCConfig”.
public class DbConfig : NinjectModule
{
public override void Load()
{
var db = "CoreConcepts".CreateSql2012Db();
Kernel.Bind<ISisoDatabase>()
.ToMethod(ctx => db)
.InSingletonScope();
db.CreateIfNotExists();
Kernel.Bind<ISession>()
.ToMethod(ctx => db.BeginSession())
.InRequestScope();
}
}
After that we just need to ensure the module is loaded by Ninject in the bootstraper, which is installed by the Ninject.MVC3 NuGet, under “App_Start”. The only change that is needed is adding one row to the “CreateKernel” member, so that it look like this:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
//ADD THIS TO LOAD OUR MODULE(s)
kernel.Load(typeof(MvcApplication).Assembly);
RegisterServices(kernel);
return kernel;
}
We are now all set and can take a dependency on “ISession” in our controllers. Either in the constructor or by resolving it using a static class/method/service locator concept against the Ninject IoC-container. Since my sample uses Db-access in each action, I used constructor injection.
public class CustomerController : Controller
{
private readonly ISession _dbSession;
public CustomerController(ISession dbSession)
{
_dbSession = dbSession;
}
//...
}
That’s it.
//Daniel