title: Onion Architecture - Service Layer Responsibility
body: <p>I am learning Onion Architecture by Jeffrey Palermo for more than 2 weeks now. I have created a test project by following <a href="http://www.develop.com/onionarchitecture" rel="nofollow noreferrer">this tutorial</a>. While studying I came across <a href="https://stackoverflow.com/questions/12317126/onion-architecture-repository-vs-service">this</a> question on SO. According to accepted answer, one person <code>nwang</code> suggests that Methods like GetProductsByCategoryId should not be in Repository and one the other hand <code>Dennis Traub</code>
  suggests that it is the responsibility of the Repository. What I am doing is : </p>
  
  <p>I have a General Repository in <code>Domain.Interface</code> in which I have a method <code>Find</code> : </p>
  
  <pre><code>public interface IRepository&lt;TEntity&gt; where TEntity : class
  {
       IEnumerable&lt;TEntity&gt; Find(Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null);
       .......
       .......
       .......
  }
  </code></pre>
  
  <p>Then I created a <code>BaseRepository</code> in <code>Infrastucture.Data</code>:</p>
  
  <pre><code>public class RepositoryBase&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class
  {
       internal readonly DbSet&lt;TEntity&gt; dbSet;
       public virtual IEnumerable&lt;TEntity&gt; Find(
              Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null)
       {
              IQueryable&lt;TEntity&gt; query = dbSet;
  
              if (filter != null)
              {
                  query = query.Where(filter);
              }
              return query.ToList();
       }
  }
  </code></pre>
  
  <p>And I have a <strong>concrete repository</strong> in <code>Infrastructure.Data</code></p>
  
  <pre><code>public class ProductRepository : RepositoryBase&lt;Product&gt;, IProductRepository
  {
        public ProductRepository(MyDBContext context)
             : base(context)
         {         
  
         }
  }
  </code></pre>
  
  <p>Now what I am doing in my Service Layer is Injecting Repository into Service and calling <code>Repository.Find</code> for methods like <code>GetProductsByCategoryId</code>. Like :</p>
  
  <pre><code>public class ProductService : IProductService
  {
       private readonly IUnitOfWork _unitOfWork;
       private readonly IProductRepository _productRepository;
  
       public ProductService(IUnitOfWork unitOfWork, IProductRepository productRepository)
       {
            _unitOfWork = unitOfWork;
            _productRepository = productRepository;
       }
  
       public IList&lt;Product&gt; GetProductsByCategoryId(int CategoryId)
       {
            // At the moment, My code is like this:
            return _productRepository.Find(e =&gt; 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?
           }
      }
  </code></pre>