Here is a quick draft of what I'd try...I would create these layers:
- Contoso.Core (Class Library)
- Contoso.Data (Class Library)
- Contoso.Service (Class Library)
- Contoso.Web.Framework (Class Library)
- Contoso.Web (ASP.NET MVC 5.0)
Contoso.Core:
This layer holds all my entities/classes representing my database TABLES.
So for example, I would have a:
- User.cs class.
- Product.cs class
- ProductDetail.cs class
- Etc..
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 my DbSet<>
defined.
So for example, I would have the following inside my ContosoDbContext.cs
:
- public DbSet User { get; set; }
- public DbSet Product { get; set; }
- public DbSet ProductDetail { get; set; }
Needless to say, the Contoso.Data layer WILL HAVE A DEPENDECY on the Contoso.Core
layer.
In addition, it is inside that Contoso.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 a Login()
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 the Contoso.Core
layer), I WILL ALSO HAVE A DEPENDENCY on the Contoso.Core
layer.
Contoso.Web.Framework:
This layer would have a dependency on the Contoso.Core
, Contoso.Data
and Contoso.Service
.
I would use this Contoso.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 the Contoso.Service
AND on the Contoso.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 the Contoso.Service
and Contoso.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 on Microsoft.AspNet.Identity.EntityFramework
in addition to the Microsoft.AspNet.Identity.Core
Likewise, my ContosoDbContext.cs
class would need to implement the IdentityDbContext
interface which happens to belong to the Microsoft.AspNet.Identity.EntityFramework.dll
.
This means my Contoso.Data
layer would have a dependency on Microsoft.AspNet.Identity.EntityFramework
and most probably the Microsoft.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 the Contoso.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