Dot-Net

將 ASP.NET 身份與核心域模型解耦 - 洋蔥架構

  • July 19, 2017

我正在使用這個範例項目(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”類中,則創建的數據庫將有四個新表,而不是身份框架的預設五個表。

身份表的洋蔥數據庫問題

我需要將這些表合併到預設表中。如何 ?

編輯:

這是駐留在數據層中的身份模型(我無法從核心中引用)。

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);

好的,我通過執行以下操作解決了這個問題:

  1. 在核心中包含對 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 上上傳完整的工作範例。

引用自:https://stackoverflow.com/questions/26580209