title: Just how much activity can take place in a service and repository layer?
body: <p>I am using <code>ASP.NET MVC 3</code>.</p>
  
  <p>I'm still confused as to how much can happen in a repository layer.  I've always been under the assumption that it must just do what is needed and nothing more.</p>
  
  <p>I have always worked with a service layer that in turn calls my repository layer.  I always had a corresponding service layer for a repository layer, and a corresponding service layer method for a repository method, even if it just to get a list of items.  So I would have the following service layer:</p>
  
  <pre><code>public class UnitService : IUnitService
  {
       private readonly IUnitRepository unitRepository;
  
       public UnitService(IUnitRepository unitRepository)
       {
            this.unitRepository = unitRepository;
       }
  
       public IEnumerable&lt;Unit&gt; GetAll()
       {
            return unitRepository.GetAll();
       }
  }
  </code></pre>
  
  <p>This is what my repository layer could look like:</p>
  
  <pre><code>public class UnitRepository : IUnitRepository
  {
       // Partial code
  
       public IEnumerable&lt;Unit&gt; GetAll()
       {
            return DbContext.Units
                 .Include("Department")
                 .OrderBy(x =&gt; x.Name);
       }
  }
  </code></pre>
  
  <p>I am trying to reduce my service layer classes if it isn't really needed.  So would it be ok just to call the repository from my controller instead of the service layer?  Does it really matter if I do it my way or not?  I mean it still brings back all my data.</p>
  
  <p>Where would I do caching and logging?  Service layer or in the repository?  Let say that the GetAll methods brings back alot of records that needs to be cached.  Would I cache this in the repository or would I need a service layer for this?</p>
  
  <p>The same goes for logging.  Where would I log stuff like:</p>
  
  <pre><code>Log.Info("Adding category: {0}, {1}", entity.Id, entity.Name);
  // adding code
  Log.Info("Category added: {0}, {1}", entity.Id, entity.Name);
  </code></pre>