洋葱架构身份框架

Onion Architecture Identity Framework

我正在关注 Onion Architecture。我正在使用 ASP.NET Identity Framework。

这是我的项目结构:

1-Core
     - Domain Classes //It contains my T4 template classes
          -- AppUser  //It is Identity User.  
     - Repository Interfaces
     - Service Interfaces
2-Infrastructure
     - Data //It contains my edmx file, I am using Db First approach.
     - Dependency Injection
     - Repository Interfaces Implementation
     - Service Interfaces Implementation
3-WebApi
     - Web Api Project
4-WebClient
     - My AngularJs App
5-Test
     - Test Project

我已经复制了 ASP.NET 身份表的脚本并在我的 SQL 服务器上执行。哪个为我创建了身份表。

在我的 Infrastructure.Data 项目中,我创建了一个 Edmx File 并在其中插入了身份表。然后我将我的 T4 template 移动到 Core.Domain,在这里我为我的 AppUser 和其他身份表创建了 Partial Class

// This make my Core.Domain project dependent on `Microsoft.AspNet.Identity`
// Which is a violation. 
public partial class AppUser : IdentityUser 
{
    //My Custom Properties
}

现在我对核心中的 AppUser Class 感到困惑。根据 Onion Architecture 标准,整个层不应依赖于任何外部库。但在这里我使用 'Microsoft.AspNet.Identity' 以使我的用户成为身份用户。有解决办法吗?

AppUser 从 IdentityUser 派生的问题现在是您的核心项目依赖于它为 Asp.Net 设计的框架,您的核心项目可能会变得有偏见。你是正确的,核心(即中央)项目应该是平台独立的。

如果您想使用 IdentityUser,您可以在 WebClient/WebApi 项目中定义一个 User 实体,它继承自 IdentityUser 并将它们保留在这些表示层中,远离核心。

要将您的演示用户实体传送到核心,您可以手动将属性映射到您的核心 AppUser 或使用像 AutoMapper

这样的映射工具

编辑

包含图表:

1-Core
    - Domain Classes 
        -- AppUser    
    - Repository Interfaces
    - Service Interfaces
2-Infrastructure
    - Data //It contains my edmx file, I am using Db First approach.
    - Dependency Injection
    - Repository Interfaces Implementation
    - Service Interfaces Implementation
3-WebApi
    - Web Api Project
        - Models
            ApiUser : *(Inherits Api Identity framework)*
4-WebClient
    - My AngularJs App
        - Models
            WebUser : *(Inherits IdentityUser)*
5-Test
    - Test Project