解耦 ASP.NET MVC 5 Identity 以允许实现分层应用程序 [英] Decoupling ASP.NET MVC 5 Identity to allow implementing a layered application

查看:31
本文介绍了解耦 ASP.NET MVC 5 Identity 以允许实现分层应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ASP.NET MVC 的新手,我一直在开发具有个人用户身份验证的 MVC 5 应用程序.我在做我的应用程序时一直在做一个分层模式,比如分离模型层、DAL 层、Repos 等.但现在在 MVC 5 中,我希望能够使用他们称之为身份的用户和角色管理和身份验证,然后仍然具有我的应用程序的分层结构,因为现在看来 Identity 几乎与 MVC 项目本身以及其中的用户和角色模型以及上下文耦合在一起.

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.

我现在在我的应用程序中所做的是我在 MVC 项目(在单独的文件夹中!)中拥有所有我应该独立的层,例如我的 DAL、UnitOfWork、Repos、其他模型等工作,现在.我知道这不是正确的做法.

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(类库)
  • Contoso.Data(类库)
  • Contoso.Service(类库)
  • Contoso.Web.Framework(类库)
  • Contoso.Web (ASP.NET MVC 5.0)

Contoso.Core:

这一层包含代表我的数据库表的所有实体/类.

This layer holds all my entities/classes representing my database TABLES.

例如,我会有一个:

  • User.cs 类.
  • Product.cs 类
  • ProductDetail.cs 类
  • 等等.

有些人称这些实体/类为:领域对象,其他人称之为 POCO 类.

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:

这一层是我定义我的 ContosoDbContext.cs 类的地方.我在该文件中定义了所有 DbSet<>.例如,我的 ContosoDbContext.cs 中有以下内容:

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:

  • 公共 DbSet 用户 { get;放;}
  • public DbSet Product { get;放;}
  • public DbSet ProductDetail { get;放;}

不用说,Contoso.Data 层将依赖于 Contoso.Core 层.此外,在 Contoso.Data 层内,我将拥有通用存储库以及与数据访问"相关的任何内容.

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:

这一层将是我放置所有业务规则的地方.例如,我可能有一个 UserService.cs 类,它可以有一个 Login() 方法.Login() 方法将接收用户名/密码并调用存储库来查找用户.

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.

因为服务层需要存储库,所以我将依赖 Contoso.Data 层并且因为我将使用 User 类(它恰好位于 Contoso.Core 层),我还将依赖于 Contoso.Core 层.

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:

该层将依赖于 Contoso.CoreContoso.DataContoso.Service.我会使用这个 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:

最后一层,MVC 5.0 应用程序,将依赖于 Contoso.Web.FrameworkContoso.ServiceContoso.Core 层.

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.

控制器将调用位于 Contoso.Service 层中定义的类中的方法(例如 Login() 方法).

The Controllers, would invoke methods living inside the classes defined in your Contoso.Service layer (for example the Login() method).

Login() 方法可能会也可能不会,例如,返回一个 User 类(空的或填充的)并且因为它返回一个 User 类并且因为我们在一个控制器中,所以我们的 Contoso.Web 层需要依赖于 Contoso.ServiceContoso.Core.

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.

到目前为止,我还没有回答你的问题,但我对 MVC 5.0 及其新的身份机制知之甚少,我相信 Contoso.Core 层需要依赖于 Microsoft.AspNet.Identity.EntityFramework 除了 Microsoft.AspNet.Identity.Core

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

同样,我的 ContosoDbContext.cs 类需要实现 IdentityDbContext 接口,该接口恰好属于 Microsoft.AspNet.Identity.EntityFramework.dll.

Likewise, my ContosoDbContext.cs class would need to implement the IdentityDbContext interface which happens to belong to the Microsoft.AspNet.Identity.EntityFramework.dll.

这意味着我的 Contoso.Data 层将依赖于 Microsoft.AspNet.Identity.EntityFramework 并且很可能依赖于 Microsoft.AspNet.Identity.Core 以及...

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

正如您所说,当您创建一个新的 MVC 5.0 项目时,所有这些都存在并在单个应用程序中定义.没有任何东西被或已经被解耦到层中.因此,在上述架构中,ContosoDbcontext.cs 类位于 Contoso.Data 层内,而不是直接位于 ASP.NET MVC 应用程序内.

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.

由于我没有尝试过新的 ASP.NET Identity,也没有尝试解耦,所以我不知道如何诚实地回答你的问题.我想你必须试着移动一些东西.

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

文斯

这篇关于解耦 ASP.NET MVC 5 Identity 以允许实现分层应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆