0

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.

I don't like VS2012 MVC4 template because it directly uses database access (db initialization, for example).

My project divides in:

  • a UI project (the mvc4 application), with Forms Authentication

  • 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).

I looked at this example:

http://kevin-junghans.blogspot.it/2013/03/decoupling-simplemembership-from-your.html

related to this question

SimpleMembership - anyone made it n-tier friendly?

And now I have some question:

  • 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 ...

  • I always have to initialize WebMatrix to directly access DB? This piece of code:

        public static void Register()
    {
        Database.SetInitializer<SecurityContext>(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);
    }
    

breaks my decoupling with the Domain .. how can i make WebSecurity using my UnitOfWork for example? what is the best practice?

  • 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 ... Custom principal in ASP.NET MVC

Thank you!

Community
  • 1
  • 1
Marco S.
  • 137
  • 8

1 Answers1

2

You have a lot of questions here Marco. Let me take a stab at them.

How to inject a UOW

Static classes and dependency injection do not mix well, as pointed out in this QA. When I first went through this exercise of decoupling SimpleMembership the concentration was just on decoupling from the domain, as discussed in the article you referenced. It was just a first step and it can be improved on, including making it easier for dependency injection. I debated whether to make WebSecurity static or not and went with static because that is how the original SimpleMembership is implemented, making it a more seamless transition for user of the SimpleSecurity. SimpleSecurity is an open source project and contributions are welcome. Making it non-static would not be difficult and probably makes sense in the long run. Once it is made non-static we could use a Factory pattern to create the UnitOfWork and inject the appropriate Factory.

Why do I have to Register WebSecurity?

SimpleSecurity is just a wrapper around the WebMatrix WebSecurity classes, which require initialization. The Register method just makes sure that WebMatrix is initialized and initializes our database. I disagree that having this method call in the Globa.asax couples it with the Domain in any way. Having it work with your UnitOfWork should have nothing to do with the Application Domain, or with having to call a Register method at application start-up.

How can I store additional data (ex: email) and retrieve it, without performing a database query every time?

This is actually accomplished quite easy in .NET 4.5 by using ClaimsPrincipal. All principals in .NET 4.5 inherit from ClaimsPrincipal, which allows you to store information in the principal as claims. Claims are basically key value pairs that let you store any type of data on the user. For example in ASP.NET the roles for a user are stored as claims. To add your own claims you need to do something called claims transformation. Then to retrieve the information you can create a custom claims principal. Adding this to SimpleSecurity would be a nice feature.

Community
  • 1
  • 1
Kevin Junghans
  • 17,475
  • 4
  • 45
  • 62
  • Thank you man. Now it sounds a bit cleaner. I understand your point of view, but registering WebMatrix to inizialize the DB sounds a little dirty to me. My users table is part of the domain, I implemented my domain access throught the UnitOfWork .. Why shouldn't WebMatrix use my DAL to access user informations? Why should it be "directly" connected to the database? I'm assuming that I use a Model-First approach, my Domain doesn't need to be initialized ... and of course, this is MY scenario. Btw, thank you again for your answer! – Marco S. Mar 28 '13 at 10:16