将 ASP.NET 身份与核心域模型解耦 - 洋葱架构 [英] Decoupling ASP.NET Identity from the Core Domain Models - Onion Architecture

查看:16
本文介绍了将 ASP.NET 身份与核心域模型解耦 - 洋葱架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个示例项目(https://github.com/imranbaloch/ASPNETIdentityWithOnion)作为在我的应用程序架构中,在这个示例中,核心完全从基础设施(包括身份框架)中解构出来.

I am using this sample project (https://github.com/imranbaloch/ASPNETIdentityWithOnion) as my application architecture, in this sample the core is completly decoplied from the infrastrure including the identity framework.

在这个示例中,作者使用适配器模式来解耦核心身份类(IdentityUser、IdentityRole ...),并在核心层提供类似的类.

In this sample the author has used the adapter pattern to decouple core identity classes (IdentityUser, IdentityRole ... ) and provide classes like them in the Core layer.

现在这个示例项目中的问题是域模型(产品、图像)没有与模仿身份的虚拟类(AppUser、ApplicationRole、AppliationUserRoles...)链接.

Now the issue in this sample project is that the Domain model (Product, Images) are not linked with the dummy classes (AppUser, ApplicationRole, AppliationUserRoles, ...) that mimics the Identity ones.

然后我修改了代码,添加了对 AppUser 的引用

Then I have modified the code to added the reference to AppUser

public sealed class Image : BaseEntity
{
    public Image()
    {
        Products = new HashSet<Product>();
    }

    public string Path { get; set; }

    public AppUser AppUser { get; set; } // The  Added Reference ...

    public ICollection<Product> Products { get; set; }
}

如果我将AppUser"导航属性放在Image"类中,则创建的数据库将有四个新表,而不是身份框架的默认五个表.

If I put the "AppUser" navigation property inside the "Image" class, the created database will have FOUR new tables other than the default FIVE tables of the identity framework.

我需要将这些表合并到默认表中.怎么样?

I need to merge these tables into the default ones. how ?

这是驻留在数据层中的身份模型(我无法从核心引用).

This is the Identity Models that resides in the Data Layer (which I can not reference from the core).

public class ApplicationIdentityUser :
    IdentityUser<int, ApplicationIdentityUserLogin, ApplicationIdentityUserRole, ApplicationIdentityUserClaim>, IDomainUser {

    public ApplicationIdentityUser()
        : base() {
        Images = new HashSet<Image>();
    }

    public string Name { get; set; }
    public virtual ICollection<Image> Images { get; set; }
}


public class ApplicationIdentityRole : IdentityRole<int, ApplicationIdentityUserRole>
{
    public ApplicationIdentityRole(){}

    public ApplicationIdentityRole(string name){Name = name;}
}

public class ApplicationIdentityUserRole : IdentityUserRole<int> {}

public class ApplicationIdentityUserClaim : IdentityUserClaim<int>{}

public class ApplicationIdentityUserLogin : IdentityUserLogin<int>{}

这也是我在 OnModelCreating 方法中的模型构建器:

Also this is my model builder in the OnModelCreating method:

  modelBuilder.Entity<Image>()
            .Property(e => e.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<Image>()
            .HasMany(e => e.Products)
            .WithRequired(e => e.Image)
            .WillCascadeOnDelete(false);
        modelBuilder.Entity<ApplicationIdentityUser>()
             .Property(e => e.Id)
             .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<ApplicationIdentityRole>()
            .Property(e => e.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<ApplicationIdentityUserClaim>()
             .Property(e => e.Id)
             .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

推荐答案

好的,我已经通过以下方式解决了这个问题:

Okay I have solved this by doing the following:

  1. 在核心中包含对 Microsoft.AspNet.Identity.Core 的依赖项
  2. 在AppUser上实现IUser接口(该接口来自Microsoft.AspNet.Identity.Core).
  3. ApplicationRole上实现IRole接口.
  4. 完全摆脱 IdentityDbContext 并仅从 DbContext 继承.
  5. 实现您自己的 IUserStore* 版本,提供您的 AppUser
  6. 实现您自己的 IRoleStore 版本,提供您的 ApplicationRole.
  1. Include a dependency in your Core to Microsoft.AspNet.Identity.Core
  2. Implement IUser interface on the AppUser (this interface come from Microsoft.AspNet.Identity.Core).
  3. Implement IRole interface on the ApplicationRole.
  4. Completely get rid of the IdentityDbContext and inherit only from DbContext.
  5. Implement your own version of IUserStore* providing your AppUser
  6. Implement your own version of IRoleStore providing your ApplicationRole.

我知道依赖 Microsoft.AspNet.Identity.Core 听起来很奇怪,但我们只需要 IUser 接口,它基本上也被视为您的应用程序的核心域模型.

I know that making a dependency on the Microsoft.AspNet.Identity.Core sounds Odd, but we only need the interface IUser which is basically considered as Core Domain Model for your application too.

这里的终极想法是彻底摆脱 Microsoft.AspNet.Identity.EntityFramework.

有兴趣的开发者可以为此 +1,所以我可以在 GitHub 上上传完整的工作示例.

Interested devs can +1 this, so I can upload a full working sample on GitHub.

这篇关于将 ASP.NET 身份与核心域模型解耦 - 洋葱架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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