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.

private EFDbContext context = new EFDbContext();<br />

}<br />

}<br />

public IEnumerable Products {<br />

get { return context.Products; }<br />

}<br />

This is the repository class. It implements the IProductRepository interface and uses an instance of<br />

EFDbContext to retrieve data from the database using the Entity Framework. You will see how I work with the Entity<br />

Framework (and how simple it is) as I add features to the repository.<br />

To use the new repository class, I need to edit the Ninject bindings and replace the mock repository with a binding for the real<br />

one. Edit the NinjectDependencyResolver.cs class file in the SportsStore.WebUI project so that the<br />

AddBindings method looks like Listing 7-15.<br />

Listing 7-15. Adding the Real Repository Binding in the NinjectDependencyResolver.cs File<br />

using System;<br />

using System.Collections.Generic;<br />

using System.Linq;<br />

using System.Web.Mvc;<br />

using Moq;<br />

using Ninject;<br />

using SportsStore.Domain.Abstract;<br />

using SportsStore.Domain.Concrete;<br />

using SportsStore.Domain.Entities;<br />

namespace SportsStore.WebUI.Infrastructure {<br />

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 />

kernel.Bind().To();<br />

}<br />

The new binding is in bold. It tells Ninject to create instances of the EFProductRepository class to service requests<br />

for the IProductRepository interface. All that remains now is to run the application again. Figure 7-13 shows the<br />

results, which demonstrate the application is getting its product data from the database, rather than the mock repository.<br />

182

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

Saved successfully!

Ooh no, something went wrong!