这是 pro asp .net 书中的一个示例,但我无法弄清楚出了什么问题。我已经阅读了许多具有相同问题的帖子,但它并没有解决我的问题。
该错误表明 IProductRepository 由于受到保护而无法访问,但我没有将任何内容设置为私有。我不明白
这是我的产品类
namespace SportStore.Domain.Entities
{
class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
}
ninject的加法
namespace SportsStore.WebUI.Infrastructure
{
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext
requestContext, Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
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 }
}.AsQueryable());
ninjectKernel.Bind<IProductRepository>().ToConstant(mock.Object);
}
}
}
界面
namespace SportStore.Domain.Abstract
{
interface IProductRepository
{
IQueryable<Product> Products { get; }
}
}