vreksam/WebApi/App_Start/NinjectWebCommon.cs

83 lines
2.6 KiB
C#
Raw Normal View History

2017-06-12 13:37:48 +03:00
using BL.Interfaces;
using DAL.Repositories;
2017-06-12 12:46:00 +03:00
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(WebApi.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(WebApi.App_Start.NinjectWebCommon), "Stop")]
namespace WebApi.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
2017-06-12 13:37:48 +03:00
using System.Web.Http;
using Ninject.Web.WebApi;
using DAL;
using DAL.Interfaces;
using BL.Services;
2017-06-12 12:46:00 +03:00
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();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
2017-06-12 13:37:48 +03:00
// Support WebAPI
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
2017-06-12 12:46:00 +03:00
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
2017-06-12 13:37:48 +03:00
kernel.Bind<IFoorumDbContext>().To<FoorumDbContext>().InRequestScope();
kernel.Bind<IFoorumRepository>().To<FoorumRepository>().InRequestScope();
kernel.Bind<IPostRepository>().To<PostRepository>().InRequestScope();
kernel.Bind<IFoorumService>().To<FoorumService>().InRequestScope();
}
2017-06-12 12:46:00 +03:00
}
}