将ASP.NET身份与核心域模型分离-洋葱体系结构 [英] Decoupling ASP.NET Identity from the Core Domain Models - Onion Architecture

查看:100
本文介绍了将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 ...),并在Core层中提供了类似的类.

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. 在您的Core中包括对Microsoft.AspNet.Identity.Core的依赖
  2. 在AppUser上
  3. 实施 IUser 界面(此界面来自 Microsoft.AspNet.Identity.Core).
  4. ApplicationRole 上实现 IRole 界面.
  5. 完全摆脱 IdentityDbContext ,仅继承 DbContext .
  6. 提供您的 AppUser
  7. ,以实现您自己的 IUserStore * 版本
  8. 实施您自己的 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天全站免登陆