29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

public class NinjectDependencyResolver : IDependencyResolver {<br />

private IKernel kernel;<br />

public NinjectDependencyResolver(IKernel kernelParam) {<br />

kernel = kernelParam;<br />

AddBindings();<br />

}<br />

public object GetService(Type serviceType) {<br />

return kernel.TryGet(serviceType);<br />

}<br />

public IEnumerable GetServices(Type serviceType) {<br />

return kernel.GetAll(serviceType);<br />

}<br />

}<br />

}<br />

private void AddBindings() {<br />

// put bindings here<br />

}<br />

As you may recall from Chapter 6, the next step is to create a bridge between the NinjectDependencyResolver<br />

class and the MVC support for dependency injection in the App_Start/NinjectWebCommon.cs file, which one of<br />

the Ninject NuGet packages added to the project, as shown in Listing 7-2.<br />

Listing 7-2. Integrating Ninject in the NinjectWebCommon.cs File<br />

...<br />

private static void RegisterServices(IKernel kernel) {<br />

System.Web.Mvc.DependencyResolver.SetResolver(new<br />

SportsStore.WebUI.Infrastructure.NinjectDependencyResolver(kernel));<br />

}<br />

...<br />

Running the Application<br />

If you select Start Debugging from the Debug menu, you will see an error page as shown in Figure 7-3. This is<br />

because you have requested a URL associated with a non-existent controller.<br />

166

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!