[英]How to skip passing type in implementing c# non-generic interface generic property?
我正在使用“清潔架構”解決方案模板,我相信它非常有前途,但是,我在分離從IdentityUser
派生的ApplicationUser
類時遇到了問題,因為它們都是基礎結構層的一部分,而且我有一個在定義引用ApplicationUser
的帳戶實體時,依賴於它在核心層“域”上,所以當我從內到外以一種方式進行引用時,我不能在那里使用它,所以我傳遞了一個通用類型,但是我仍然無法在基礎設施中實現ApplicationDbContext
,有什么想法嗎?
賬戶.cs
using MacSys.Domain.Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace MacSys.Domain.Entities
{
public class Account<T> : LocaleEntityBase
{
public Account()
{
ApplicationUsers = new List<T>();
}
public City City { get; set; }
public string PhoneNumber { get; set; }
public IList<T> ApplicationUsers { get; set; }
}
}
IApplicationDbContext.cs
using MacSys.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using System.Threading;
using System.Threading.Tasks;
namespace App.Application.Common.Interfaces
{
public interface IApplicationDbContext
{
DbSet<Country> Countries { get; set; }
DbSet<City> Cities { get; set; }
DbSet<AccountType> AccountTypes { get; set; }
DbSet<Configuration> Configurations { get; set; }
DbSet<Account<??>> Accounts { get; set; }
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}
}
應用程序數據庫上下文.cs
using App.Application.Common.Interfaces;
using App.Domain.Entities;
using App.Infrastructure.Identity;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using App.Infrastructure.Identity.ApplicationGroups;
namespace App.Infrastructure.Persistence
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser,
ApplicationRole,
string>, IApplicationDbContext
{
private readonly ICurrentUserService _currentUserService;
private readonly IDateTime _dateTime;
public ApplicationDbContext(
DbContextOptions options,
ICurrentUserService currentUserService,
IDateTime dateTime) : base(options)
{
_currentUserService = currentUserService;
_dateTime = dateTime;
}
public DbSet<Country> Countries { get; set; }
public DbSet<City> Cities { get; set; }
public DbSet<AccountType> AccountTypes { get; set; }
public DbSet<Configuration> Configurations { get; set; }
public DbSet<Account<ApplicationUser>> Accounts { get; set; }
}
}
我在 ApplicationDbContext 類實現中遇到錯誤,因為我無法在項目之間進行雙重引用的情況下找到兩個屬性之間的關系。
將域與框架分離的一種常見方法是根本不耦合它們。 您實際上不必將所有實體都放在同一個數據庫上下文中,也不需要讓您的帳戶實體從ApplicationUser
繼承。
相反,您可以做的是將這兩個問題完全分開:使用標准IdentityDbContext
及其IdentityUser
作為主要用戶身份進行身份驗證和授權。 但為您的域維護一個單獨的Account
實體。 您甚至可以共享相同的用戶 ID 以獲得一對一的映射。 如果您隨后將Account
和域模型的其余部分放入其自己的數據庫上下文中,那么您的域實體將與應用程序的身份驗證框架層完全分離。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.