title: Repository and static methods
body: <p>Recently I adopted the Repository/Service design pattern to handle objects in my MVC 4 project.</p>
  
  <p>It was definitely a good move, but I have found an issue :) Now this could be down to my application of the pattern, so I thought I would put it out there and see if anyone can suggest a solution or a design pattern to fit my needs.</p>
  
  <p>I have a Repository and a Service to handle custom users. This works perfectly for Getting users and performing any user method. But, before I moved to the Repository/Service design pattern I had a static class that handled all of the user related methods.</p>
  
  <p>One of these methods was:</p>
  
  <pre><code>public static Profile CurrentUser()
  {
      // TODO: Need to replace the Profile session when any permissions are changed....
      var companyId = HttpContext.Current.Session["CompanyId"].ToString();
  
      if (HttpContext.Current.User.Identity.IsAuthenticated &amp;&amp; !string.IsNullOrEmpty(companyId))
      {
          var userId = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString();
          var repository = new ProfileRepository(companyId);
  
          return repository.Get(userId);
      }
      else
          return null;
  }
  </code></pre>
  
  <p>As you can see, this method returns the Profile of a particular user if they are logged in. If they are not, then it returns null.</p>
  
  <p>My issue is that that method needs to be seperate from the Service and Repository because it has to be called from any controller which is why I made it static.</p>
  
  <p>To solve this issue I created a class called ProfileContext and CurrentUser() is the only method in this static class.</p>
  
  <p>So my question is simple. Would this be the best way to handle the CurrentUser method or is there another, better solution?</p>
  
  <p>Cheers,
  /r3plica</p>