I'm new to ASP.NET MVC and I've been developing a MVC 5 application with individual user authentication. I've been doing a layered pattern when doing my applications like separating Model layer, DAL layer, Repos, etc. etc. but now in MVC 5, I want to be able to use the user and role management and authentication which they call Identity, and then still have that layered structure to my application because right now it seems Identity is pretty much coupled with the MVC project itself with the user and role models in there and the context too.
What I did in my application for now is I have all my supposed-to-be-separate layers like my DAL, UnitOfWork, Repos, other models, etc in the MVC project (in separate folders!) just to make it work, for now. And I know it's just not the right way to do it.
So can anyone point me to some good examples or articles about this or explain it directly if it's possible or not and how? Google hasn't been friendly to me about this one. Thanks!
Here is a quick draft of what I'd try...I would create these layers:
Contoso.Core:
This layer holds all my entities/classes representing my database TABLES.
So for example, I would have a:
Some people call these entities/classes: the Domain Objects, others call it the POCO classes.
Either or, these entities/classes are defined in the Core Layer since they may (or may not) be used amongst the other layers.
Contoso.Data:
This layer is where I define my
ContosoDbContext.cs
class. It is inside that file that I have all myDbSet<>
defined. So for example, I would have the following inside myContosoDbContext.cs
:Needless to say, the Contoso.Data layer WILL HAVE A DEPENDECY on the
Contoso.Core
layer. In addition, it is inside thatContoso.Data
layer that I would have my Generic Repository and anything related to "data access".Contoso.Service:
This layer would be where I place all my business rules. For example, I may have a
UserService.cs
class that could have aLogin()
method. The Login() method would receive a username/password and call the Repository to lookup the user.Because the Service layer needs the Repository, I WILL HAVE A DEPENDENCY on the
Contoso.Data
layer AND because I'll be playing around with the User class (which happens to live inside theContoso.Core
layer), I WILL ALSO HAVE A DEPENDENCY on theContoso.Core
layer.Contoso.Web.Framework:
This layer would have a dependency on the
Contoso.Core
,Contoso.Data
andContoso.Service
. I would use thisContoso.Web.Framework
layer to configure my Dependency Injection.Contoso.Web:
The final layer, the MVC 5.0 application, would have a dependency on the
Contoso.Web.Framework
AND on theContoso.Service
AND on theContoso.Core
layers.The Controllers, would invoke methods living inside the classes defined in your
Contoso.Service
layer (for example the Login() method).The Login() method may or may not, for example, return a User class (null or populated) and because it returns a User class AND BECAUSE we are inside a Controller, our
Contoso.Web
layer needs a dependency on theContoso.Service
andContoso.Core
.Of course, I haven't detailed everything here or every layer but this is just to give you an example of the type of architecture I’d use.
So far, I haven't answered your question but with little I know about MVC 5.0 and its new Identity mechanism, I believe the
Contoso.Core
layer would need to have a dependency onMicrosoft.AspNet.Identity.EntityFramework
in addition to theMicrosoft.AspNet.Identity.Core
Likewise, my
ContosoDbContext.cs
class would need to implement theIdentityDbContext
interface which happens to belong to theMicrosoft.AspNet.Identity.EntityFramework.dll
.This means my
Contoso.Data
layer would have a dependency onMicrosoft.AspNet.Identity.EntityFramework
and most probably theMicrosoft.AspNet.Identity.Core
as well...As you say, when you create a new MVC 5.0 project, all of this exist and is defined within the single application. Nothing is or has been decoupled into layers. So in the above architecture the
ContosoDbcontext.cs
class lives inside theContoso.Data
layer and NOT directly inside the ASP.NET MVC application.Since I haven't tried the new ASP.NET Identity nor have I tried to decouple things around, I wouldn't know how to honestly answer your question. I guess you'll have to try and move things around.
If and when you do, feel free to tell us how it went and what are the things/problems you encountered.
Meanwhile, I hope this has helped you shed some light (or not).
Vince