namespace ShoppingApi.App_Start { using System; using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject; using Ninject.Web.Common;
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>();
namespace ShoppingApi.App_Start { using System; using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject; using Ninject.Web.Common;
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>();
public IEnumerable<object> GetServices(Type serviceType) { return kernel.GetAll(serviceType); } } }
In AddBinding() method add the binding you want to add.
Now I want to use in my controller. I have use Parametrized constructor to set the value
public class AdminController : Controller { private IProductRepository repository;
public AdminController(IProductRepository prodRepository) { repository = prodRepository; }
// GET: Admin //[AllowAnonymous] public ActionResult Index() {
return View(repository.Products); }
public ViewResult Edit(int productID) { var product=repository.Products.FirstOrDefault(p=>p.ProductId.Equals(productID)); return View(product); }
public ViewResult AddNewItem() { return View(); }
[HttpPost] public ActionResult AddNewItem(Product product) { if(ModelState.IsValid){ repository.SaveProduct(product); TempData[“Message”] = string.Format(“Item {0} is successfully Added”, product.Name); return RedirectToAction(“Index”); } else { TempData[“Message”] = string.Format(“Item {0} is not Added”, product.Name); return View(product); }
}
[HttpPost] public ActionResult Edit(Product product) { if (ModelState.IsValid) { repository.SaveProduct(product); TempData[“Message”] = string.Format(“Item {0} is successfully saved”, product.Name); return RedirectToAction(“Index”); } else { return View(product); } }
[HttpPost] public ActionResult Delete(int productID) { Product deleteProduct = repository.DeleteProduct(productID); if (deleteProduct != null) { TempData[“Message”] = string.Format(“Item {0} is successfully Deleted”, deleteProduct.Name);
} return RedirectToAction(“Index”); } }
Yes its that easy to implement. Hope that helpful for you.
Note: In WebApi we can also use Ninject but it is not the same way . This is for next article
Disclaimer: The above content reflect author’s personal views and do not reflect the views of OYEWIKI. Neither OYEWIKI nor any person/organization acting on its behalf is liable to accept any legal liability/responsibility for any error/mislead in this information or any information available on the website. This website in no way accepts the responsibility for any loss, injury, damage, discomfort or inconvenience caused as a result of reliance on any information provided on this website.
If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.