title: MVC4, UnitOfWork + DI, and SimpleAuthentication .. how to decouple?
body: <p>I'm currently working on an MVC4 project, i make use if Ninject to inject a UnitOfWork into my controllers, and I'm using UnitOfWork + Generic Repository pattern.</p>
  
  <p>I don't like VS2012 MVC4 template because it directly uses database access (db initialization, for example).</p>
  
  <p>My project divides in:</p>
  
  <ul>
  <li><p>a UI project (the mvc4 application), with Forms Authentication</p></li>
  <li><p>a Domain project (the db entities, the repositories, the UnitOfWork interface plus two UnifOfWork implementations, one with MOQ and one with EF; they are injected into UI controllers via Ninject).</p></li>
  </ul>
  
  <p>I looked at this example:</p>
  
  <p><a href="http://kevin-junghans.blogspot.it/2013/03/decoupling-simplemembership-from-your.html" rel="nofollow noreferrer">http://kevin-junghans.blogspot.it/2013/03/decoupling-simplemembership-from-your.html</a></p>
  
  <p>related to this question</p>
  
  <p><a href="https://stackoverflow.com/questions/15006532/simplemembership-anyone-made-it-n-tier-friendly/15162535#15162535">SimpleMembership - anyone made it n-tier friendly?</a></p>
  
  <p>And now I have some question:</p>
  
  <ul>
  <li><p>How can i inject my UoW here? WebSecurity class is static, there is no contructor, it directly instantiate the UoW to perform activities on db ...</p></li>
  <li><p>I always have to initialize WebMatrix to directly access DB? This piece of code:</p>
  
  <pre><code>    public static void Register()
  {
      Database.SetInitializer&lt;SecurityContext&gt;(new InitSecurityDb());
      SecurityContext context = new SecurityContext();
      context.Database.Initialize(true);
      if (!WebMatrix.WebData.WebSecurity.Initialized)
          WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection("DefaultConnection",
              "UserProfile", "UserId", "UserName", autoCreateTables: true);
  }
  </code></pre></li>
  </ul>
  
  <p>breaks my decoupling with the Domain .. how can i make WebSecurity using my UnitOfWork for example? what is the best practice?</p>
  
  <ul>
  <li>How can i store additional data (for example, EmailAddress and so on) and retrieve it, without performing a Database query everytime i have to access the User profile? Something like the old CustomPrincipal ... <a href="https://stackoverflow.com/questions/3763221/custom-principal-in-asp-net-mvc">Custom principal in ASP.NET MVC</a></li>
  </ul>
  
  <p>Thank you!</p>