title: Repositories and Services, MVC Model
body: <p>So I've been learning about the Repository model, and it seems that it is expected that Repositories do not do a lot of intricate logic. However I also read that most of the business logic should not be inside of my <code>Controllers</code>. So where do I put it? </p>
  
  <p>I've looked at some sample applications and it seems that they have <strong>another</strong> layer called <code>Services</code> that do more intricate logic for things. So how does this factor into the MVC pattern? </p>
  
  <p>Do I want to build my services to access my repositories, and then my controllers to access my services? Like this?</p>
  
  <pre><code>interface IMembershipService
  {
   bool ValidateUser(string username, string password);
   MembershipCreateStatus Create(string username, string password);
  }
  interface IMembershipRepository
  {
   MembershipCreateStatus Create(string username, string password);
  }
  
  class MembershipRepository : IMembershipRepository
  {
   public MembershipRepository(ISession session)
   {
    **// this is where I am confused...** 
   }
  }
  class MembershipService : IMembershipService
  {
   private readonly IMembershipRepository membershipRepository;
   public MembershipService(IMembershipRepository membershipRepository)
   {
    this.membershipRepository = membershipRepository;
   }
  
   public bool ValidateUser(string username, string password)
   {
     // validation logic
   }
   public MembershipCreateStatus Create(string username, string password)
   {
    return membershipRepository.Create(username, password);
   }
  }
  
  class MembershipController : Controller
  {
   private readonly IMembershipService membershipService;
  
   public MembershipController(IMembershipService membershipService)
   {
    this.membershipService = membershipService
   }
  }
  </code></pre>
  
  <p>The marked part of my code is what confuses me. Everything I have read said I should be injecting my <code>ISession</code> into my repositories. This means I could not be injecting <code>ISession</code> into my services, so then how do I do Database access from my Services? I'm not understanding what the <em>appropriate</em> process is here. </p>
  
  <p>When I put <code>ValidateUser</code> in my <code>IMembershipRepository</code>, I was told that was 'bad'. But the <code>IMembershipRepository</code> is where the database access resides. That's the intention, right? To keep the database access very minimal? But if I can't put other logic in them, then what is the point? </p>
  
  <p>Can someone shed some light on this, and show me an example that might be more viable? </p>
  
  <p>I am using <code>Fluent nHibernate</code>, <code>ASP.NET MVC 3.0</code>, and <code>Castle.Windsor</code>.</p>
  
  <p>Should I instead do something like ...</p>
  
  <pre><code>class MembershipService
  {
   private readonly IMembershipRepository membershipRepository;
  
   public MembershipService(ISession session)
   {
    membershipRepository = new MembershipRepository(session);
   }
  }
  </code></pre>
  
  <p>And never give my Controllers direct access to the <code>Repositories</code>?</p>