Skip to content

Instantly share code, notes, and snippets.

@JasonMore
Created January 17, 2013 13:58
Show Gist options
  • Save JasonMore/4556056 to your computer and use it in GitHub Desktop.
Save JasonMore/4556056 to your computer and use it in GitHub Desktop.
[assembly: WebActivator.PreApplicationStartMethod(typeof(IDM.CMS3.Web.Admin.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(IDM.CMS3.Web.Admin.App_Start.NinjectWebCommon), "Stop")]
namespace IDM.CMS3.Web.Admin.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using Ninject.Extensions.Conventions;
using System.Web.Http;
using System.Web.Http.Dependencies;
using Ninject.Syntax;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
// Set Web API Resolver
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Load<RavenModule>();
var namespaces = new string[] {
"IDM.CMS3.*"
};
kernel.Bind(x => x
.FromAssembliesMatching(namespaces)
.SelectAllClasses()
.BindAllInterfaces()
);
}
}
public class NinjectDependencyScope : IDependencyScope
{
private IResolutionRoot resolver;
internal NinjectDependencyScope(IResolutionRoot resolver)
{
Contract.Assert(resolver != null);
this.resolver = resolver;
}
public void Dispose()
{
IDisposable disposable = resolver as IDisposable;
if (disposable != null)
disposable.Dispose();
resolver = null;
}
public object GetService(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has already been disposed");
return resolver.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has already been disposed");
return resolver.GetAll(serviceType);
}
}
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(kernel.BeginBlock());
}
}
}
public class RavenModule : NinjectModule
{
public override void Load()
{
Bind<IDocumentStore>()
.ToProvider<RavenDocumentStoreProvider>()
.InSingletonScope();
Bind<IDocumentSession>()
.ToMethod(c =>
{
var resolver = c.Kernel.BeginBlock();
return resolver.Get<IDocumentStore>().OpenSession();
})
.InRequestScope();
}
public class RavenDocumentStoreProvider : Provider<IDocumentStore>
{
protected override IDocumentStore CreateInstance(IContext context)
{
IDocumentStore ds;
try
{
ds = new EmbeddableDocumentStore { ConnectionStringName = "WebCms3" };
}
catch
{
ds = new DocumentStore { ConnectionStringName = "WebCms3" };
}
//RavenProfiler.InitializeFor(ds);
// also good to setup the glimpse plugin here
ds.Initialize();
//RavenIndexes.CreateIndexes(ds);
return ds;
}
}
}
<connectionStrings>
<add name="WebCms3" connectionString="DataDir = ~\App_Data\WebCms3" />
</connectionStrings>
<connectionStrings>
<add name="WebCms3" connectionString="Url = http://localhost:8080;Database=WebCms3"/>
</connectionStrings>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment