Tuesday, December 10, 2013

MVC 4 and Ninject

Starting MVC 4 with Ninject.

In VS 2012, Go to > Tools > Library Package Manager > Manage NuGet Packages for Solution.

----
Search for and select Ninject.MVC3 then install.
----

It will create a NinjectWebCommon.cs inside App_Start

Reference your dependency resolution project / dll

Add code to instantiate implementations for your interfaces.

e.g: //IKernel kernel = new StandardKernel(
            //    new LoggingModule(typeof(App).ToString(), App.AppPath.ToString())
            //    );

            //kernel.Bind<IUserService>().To<UserService>();
            //kernel.Bind<IDialogService>().To<DialogService>();
            //kernel.Bind<IWorker>().To<AsyncWorker>();
            //kernel.Bind<IDialogService>().To<DialogService>();          
            string appPath = System.Web.Hosting.HostingEnvironment.MapPath(System.Web.HttpRuntime.AppDomainAppVirtualPath);
            var modules = new List<INinjectModule>
            {
                new LoggingModule(("Ninject"), appPath)
                //new RepositoryModule()              
            };
            kernel.Load(modules);