Onion Architecture - ответственность сервисного уровня
Я изучаю луковую архитектуру Джеффри Палермо уже более 2 недель. Я создал тестовый проект, следуяэтот урок, Во время учебы я наткнулсяэтот вопрос по ТАК. Согласно принятому ответу, один человекnwang
предполагает, что методы типа GetProductsByCategoryId не должны быть в репозитории, а с другой стороныDennis Traub
предполагает, что это ответственность Репозитария. Что я делаю, это:
У меня есть общий репозиторий вDomain.Interface
в котором у меня есть метод:Find
public interface IRepository where TEntity : class
{
IEnumerable Find(Expression filter = null);
.......
.......
.......
}
Затем я создалBaseRepository
в :Infrastucture.Data
public class RepositoryBase : IRepository where TEntity : class
{
internal readonly DbSet dbSet;
public virtual IEnumerable Find(
Expression filter = null)
{
IQueryable query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
return query.ToList();
}
}
И у меня естьконкретное хранилище вInfrastructure.Data
public class ProductRepository : RepositoryBase, IProductRepository
{
public ProductRepository(MyDBContext context)
: base(context)
{
}
}
Теперь то, что я делаю на своем уровне обслуживания - это добавление репозитория в службу и вызовRepository.Find
для таких методов, какGetProductsByCategoryId
, Подобно :
public class ProductService : IProductService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IProductRepository _productRepository;
public ProductService(IUnitOfWork unitOfWork, IProductRepository productRepository)
{
_unitOfWork = unitOfWork;
_productRepository = productRepository;
}
public IList GetProductsByCategoryId(int CategoryId)
{
// At the moment, My code is like this:
return _productRepository.Find(e => e.CategoryId == CategoryId).ToList();
// My confusion is here. Am I doing it right or I need to take this code to
// ProductRepository and call _productRepositoy.GetProductsByCategoryId(CategoryId) here instead.
// If I do this, then Service Layer will become more of a wrapper around repository. Isn't it?
// My question is : What exactly will be the responsibility of the Service Layer in Onion Architecture?
}
}