title: Proper way to dependency inject authenticated user to my repository class
body: <p>I am using a service layer with repository pattern.  The controller has a dependency on the service layer, and the service layer has a dependency on the repository.  </p>
  
  <p>I have to pass logged in user information to the repository layer for authorization purposes and am trying to determine the best approach for injecting the user information into the repository considering that I seem to have an extensive injection chain: </p>
  
  <pre><code>controller -&gt; service(s) -&gt; repositories -&gt; logged in user info.  
  </code></pre>
  
  <p>I guess the easy approach would be to pass the user information to the service methods that get called(i.e. <code>FindById(int primaryKey, User currentUser)</code>, etc.) </p>
  
  <p>But this seems very limiting and problematic down the road as opposed to injecting the User information.  </p>
  
  <p>What is the recommended approach to this problem?</p>
  
  <hr>
  
  <p>I am a little confused about how the person in the article seems to be implementing the ICurrentUserFetcher.  I assume that is would provide the extra properties that are not available from the IIdentity, but the article does not make this very clear.      </p>
  
  <pre><code>class GenericRepository&lt;T&gt;: IRepository&lt;T&gt; {
      private readonly ICurrentUserFetcher currentUserFetcher;
      public GenericRepository&lt;T&gt;(Func&lt;IIdentity&gt; currentUserFetcher) {
          this.currentUserFetcher = currentUserFetcher;
      }
      public void Update(T entity) {
          var currentUser = currentUserFetcher();
          ...
      }
  }   
  var repo = new GenericRepository&lt;Person&gt;(() =&gt; HttpContext.Current.User.Identity);
  </code></pre>