将ASP.NET身份与核心域模型分离 - 洋葱架构

Zai*_*ari 10 .net onion-architecture entity-framework-6 asp.net-identity

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

在此示例中,作者使用适配器模式来分离核心标识类(IdentityUser,IdentityRole ...),并在Core层中提供类似它们的类.

现在这个示例项目中的问题是Domain模型(Product,Images)没有与模仿Identity模型的虚拟类(AppUser,ApplicationRole,AppliationUserRoles,...)链接.

然后我修改了代码以添加对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; }
}
Run Code Online (Sandbox Code Playgroud)

如果我将"AppUser"导航属性放在"Image"类中,则创建的数据库将具有除标识框架的默认FIVE表之外的四个新表.

具有身份表的洋葱数据库问题

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

编辑:

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

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>{}
Run Code Online (Sandbox Code Playgroud)

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

  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);
Run Code Online (Sandbox Code Playgroud)

Zai*_*ari 20

好的,我通过执行以下操作解决了这个问题:

  1. 在您的Core中包含对Microsoft.AspNet.Identity.Core的依赖项
  2. 在AppUser上实现IUser接口(此接口来自Microsoft.AspNet.Identity.Core).
  3. ApplicationRole上实现IRole接口.
  4. 完全摆脱IdentityDbContext并仅从DbContext继承.
  5. 实现您自己的IUserStore*版本,提供您的AppUser
  6. 实现您自己的IRoleStore版本,提供您的ApplicationRole.

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

这里的终极理念是完全获取Microsoft.AspNet.Identity.EntityFramework.

有兴趣的开发者可以+1这个,所以我可以在GitHub上传一个完整的工作样本.

  • 我正准备开始这个旅程,因为我得出的结论是,Microsoft.AspNet.Identity.EntityFramework是很好的示例代码,但如果您打算将用户作为一流域模型,则会干扰您的域. (2认同)
  • 可以提供工作解决方案的链接吗? (2认同)