Call methods between repositories - Repository Pattern

北城余情 提交于 2019-12-08 19:25:29

问题


I'm using the Repository Pattern (like the examples in the http://www.asp.net/mvc site) in a ASP.NET MVC application. I have two repositories, one called CategoryRepository an other called ProductRepository. I also use two services, the CategoryService and ProductService to validate and call the repositories methods. I need a list of categories in ProductService, a method that return one is already implemented in the CategoryRepository. My question is, which is the correct way to call the ListCategories method that exists in CategoryRepository from ProductService? I don't want to implement another ListCategories method in the ProductRepository (DRY philosophy). Thanks.


回答1:


I would recommend rolling similar repositories into one service. So if you're creating an e-Commerce application roll up ProductRepository, CategoryRepository etc into something like CatalogService and have it host all repositories that are related.




回答2:


One option is to provide the ProductService class an instance of CategoryService.

public class ProductService {
    ICategoryService _categoryService = null;

    public ProductService(ICategoryService categoryService) {
        _categoryService = categoryService;
    }
}

You could then access the category listings from the ProductService without having to establish a direct coupling to any specific CategoryService implementation.



来源:https://stackoverflow.com/questions/669799/call-methods-between-repositories-repository-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!