我有两个非常简单的表格。 Product和ProductCategory(ProductCategory就像Product的'lookup'表)。在我的控制器上,对于我的Index()方法,我想列出产品的类别。当用户点击某个类别时,我想将我的类别传递给我的List()方法,以显示特定类别的所有产品。
我正在使用ninject DI框架;我现在有类似的东西。
private IProductCategory productCategoryRepository;
private IProduct productRepository;
public StoreController(IProductCategory productCategoryRepository)
{
this.productCategoryRepository = productCategoryRepository;
}
public ViewResult Index()
{
return View(productCategoryRepository.GetCategories());
}
public ViewResult List(string category, int page = 1) //Use default value
{
...
};
我有每个表/实体的基本存储库(即GetCategories(),GetProducts(),GetProductsByCategory..etc。)最好的方法是什么......或者如何在控制器中使用两个单独的存储库?我宁愿不通过控制器传递它们。
注意:Product和ProductCategory不被视为聚合。
答案 0 :(得分:4)
正如我前面提到的,服务层可以帮助您解决此问题。服务层是用户界面和中间层之间的契约点。这可能是我在下面显示的WCF服务或简单的服务实现。
public interface IMyProductService
{
IList<Product> GetProducts();
IList<Product> GetProductsByCategory();
IList<Category> GetCategories();
}
public class MyProductService : IMyProductService
{
IProductRepository _prodRepo;
IProductCategoryRepository _catRepo;
public MyProductService(IProductRepository prodRepo, IProductCategoryRepository catRepo)
{
_prodRepo = prodRepo;
_catRepo = catRepo;
}
// The rest of IMyProductService Implementation
}
您的MVC控制器将使用构造函数注入和您选择的DI框架来引用IMyProductService。