Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

c# - Decoupling ASP.NET MVC 5 Identity to allow implementing a layered application

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...