[英]How to skip passing type in implementing c# non-generic interface generic property?
I'm using a ' Clean Architecture ' solution template, and I believe it's very promising, however, I'm facing a problem in separating the ApplicationUser
class derived from IdentityUser
since they are both parts of the infrastructure layer and I've got a dependency on it on the core layer 'Domain' when defining an Account Entity referencing ApplicationUser
, so as I'm doing the references in one-way from inside to outside I can't use it down there, so I passed a generic type but I still can't implement the ApplicationDbContext
in the infrastructure, any thoughts?我正在使用“清洁架构”解决方案模板,我相信它非常有前途,但是,我在分离从
IdentityUser
派生的ApplicationUser
类时遇到了问题,因为它们都是基础结构层的一部分,而且我有一个在定义引用ApplicationUser
的帐户实体时,依赖于它在核心层“域”上,所以当我从内到外以一种方式进行引用时,我不能在那里使用它,所以我传递了一个通用类型,但是我仍然无法在基础设施中实现ApplicationDbContext
,有什么想法吗?
Account.cs账户.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 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);
}
}
ApplicationDbContext.cs应用程序数据库上下文.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; }
}
}
I'm having the error in the ApplicationDbContext class implementation since I can't find a relation between both properties without making dual references between projects.我在 ApplicationDbContext 类实现中遇到错误,因为我无法在项目之间进行双重引用的情况下找到两个属性之间的关系。
One common approach to separating your domain from the framework would be by not coupling them at all.将域与框架分离的一种常见方法是根本不耦合它们。 You actually do not have to have all your entities in the same database context, and you do not need to make your account entity inherit from
ApplicationUser
either.您实际上不必将所有实体都放在同一个数据库上下文中,也不需要让您的帐户实体从
ApplicationUser
继承。
Instead, what you can do is completely separate these two concerns: Use the standard IdentityDbContext
with its IdentityUser
as the primary user identity for authentication and authorization .相反,您可以做的是将这两个问题完全分开:使用标准
IdentityDbContext
及其IdentityUser
作为主要用户身份进行身份验证和授权。 But maintain a separate Account
entity for your domain.但为您的域维护一个单独的
Account
实体。 You can even share the same user id to get a one-to-one mapping.您甚至可以共享相同的用户 ID 以获得一对一的映射。 If you then put the
Account
and the rest of the domain model into its own database context, then your domain entities are completely separate from the auth framework layer of your application.如果您随后将
Account
和域模型的其余部分放入其自己的数据库上下文中,那么您的域实体将与应用程序的身份验证框架层完全分离。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.