mirror of
https://github.com/rudollee/LearningMVC.git
synced 2025-06-07 07:56:14 +00:00
46 lines
1.0 KiB
C#
46 lines
1.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
using Moq;
|
|
using Ninject;
|
|
using SportsStore.Domain.Abstract;
|
|
using SportsStore.Domain.Entities;
|
|
|
|
namespace SportsStore.WebUI.Infrastructure
|
|
{
|
|
public class NinjectDependencyResolver : IDependencyResolver
|
|
{
|
|
private IKernel kernel;
|
|
|
|
public NinjectDependencyResolver(IKernel kernelParam)
|
|
{
|
|
kernel = kernelParam;
|
|
AddBindings();
|
|
}
|
|
|
|
public object GetService(Type serviceType)
|
|
{
|
|
return kernel.TryGet(serviceType);
|
|
}
|
|
|
|
public IEnumerable<object> GetServices(Type serviceType)
|
|
{
|
|
return kernel.GetAll(serviceType);
|
|
}
|
|
|
|
private void AddBindings()
|
|
{
|
|
Mock<IProductRepository> mock = new Mock<IProductRepository>();
|
|
mock.Setup(m => m.Products).Returns(new List<Product>
|
|
{
|
|
new Product { Name = "Football", Price = 25 },
|
|
new Product { Name = "Surf board", Price = 179 },
|
|
new Product { Name = "Running shoes", Price = 95}
|
|
});
|
|
|
|
kernel.Bind<IProductRepository>().ToConstant(mock.Object);
|
|
}
|
|
}
|
|
} |