Onion Architecture Identity Framework

柔情痞子 提交于 2020-05-27 09:28:04

问题


I am following Onion Architecture. And in that I am using ASP.NET Identity Framework.

Here is my Project Structure:

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

I have copied the scripts of the ASP.NET Identity tables and executed on my SQL Server. Which created Identity Tables for me.

In my Infrastructure.Data project, I created an Edmx File and inserted Identity tables in it. Then I moved my T4 template to Core.Domain, here I created Partial Class for my AppUser and other Identity tables.

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

Now I have confusion about my AppUser Class which is in Core. According to the Onion Architecture standards, This whole layer should not a be dependent on any external library. But here I am using 'Microsoft.AspNet.Identity' in order to make my user as Identity User. Is there any solution for that?


回答1:


The problem with AppUser deriving from IdentityUser is now your core project is dependant on a framework which it designed for Asp.Net, there your core project can become biased. You are correct that the core (i.e. central) project should be platform independent.

If you want to use IdentityUser you could define a User entity within your WebClient/WebApi projects which inherit from IdentityUser and keep them within these presentation layers, away from the core.

To convey your presentation user entity to the core you can manually map the properties to your core AppUser or use a mapping tool like AutoMapper

Edit

Included Diagram:

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


来源:https://stackoverflow.com/questions/30729139/onion-architecture-identity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!