【发布时间】:2014-05-26 13:54:31
【问题描述】:
我正在使用 MVC 4 创建一个应用程序。
我有以下 Mock 存储库
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=25 },
}.AsQueryable());
ninjectKernel.Bind<IProductRepository>().ToConstant(mock.Object);
如何在我的控制器中向此存储库添加新产品?
公共类 ProductController : 控制器 { 私有 IProductRepository 存储库;
public ProductController(IProductRepository productRepository)
{
repository = productRepository;
}
public ViewResult List()
{
return View(repository.Products);
}
public ViewResult Add()
{
var newProduct = new Product
{
Name = "Sneakers", Price = 30
};
//how do I add this newProduct to the repository?
}
}
我将以下内容添加到 IProductRepository
public interface IProductRepository
{
IQueryable<Product> Products();
void AddProducts(Product product);
}
public class ProductRepository : IProductRepository
{
public IQueryable<Product> Products()
{
//how do I access the repo?
}
public void AddProducts(Product product)
{
//how do I access the repo?
}
}
【问题讨论】:
-
IProductRepository有哪些方法? -
目前没有,我已经添加了两个方法..查看代码更新..但是我如何创建它们的具体实例?如何引用存储库?
-
您是在尝试实现存储库还是为您的控制器编写测试?
-
我正在尝试实现一个存储库,但不使用持久数据,必须在内存中
-
我正在尝试使用内存数据创建一个 MVC 应用程序......这可能吗?
标签: c# .net asp.net-mvc-3 asp.net-mvc-4