gpt4 book ai didi

.net - 将 ASP.NET 身份与核心域模型解耦 - 洋葱架构

转载 作者:行者123 更新时间:2023-12-04 02:55:35 24 4
gpt4 key购买 nike

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

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

现在这个示例项目中的问题是域模型(产品、图像)没有与模仿身份的虚拟类(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; }
}

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

Onion Database Problem with Identity Tables

我需要将这些表合并到默认表中。
如何 ?

编辑:

这是驻留在数据层中的身份模型(我无法从核心中引用)。
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 方法中的模型构建器:
  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);

最佳答案

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

  • 在核心中包含对 Microsoft.AspNet.Identity.Core
  • 的依赖项
  • 实现用户 AppUser 上的接口(interface)(这个接口(interface)来自
    Microsoft.AspNet.Identity.Core)。
  • 实现 IRole 上的界面申请角色 .
  • 彻底摆脱身份数据库上下文 并仅从 继承数据库上下文 .
  • 实现您自己的 版本IUserStore* 提供您的应用用户
  • 实现您自己的 版本IRoleStore 提供您的申请角色 .

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

    这里的终极想法是 完全摆脱 Microsoft.AspNet.Identity.EntityFramework .

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

    关于.net - 将 ASP.NET 身份与核心域模型解耦 - 洋葱架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26580209/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com