【发布时间】:2020-09-20 23:00:43
【问题描述】:
我有这个类,它是我的规范模式的一部分。 关注这个link
public class SpecificationEvaluator<TEntity>
where TEntity : BaseEntity
BaseEntity 只包含 id
例如
public class BaseEntity
{
public int Id { get; set; }
}
这适用于我所有继承“BaseEntity”的实体
例如
Product : BaseEntity
Invoice : BaseEntity
Message : BaseEntity
现在我想在我的“用户”实体上使用规范模式,以便我可以提供规范/条件并获取用户列表。
但我的问题是我的项目“用户”实体没有继承“BaseEntity”,它继承了“IdentityUser”
例如
public class User : IdentityUser<int>
{
}
我知道我不能有多个类作为约束,但我可以有多个接口。
我知道我可以将约束设置为“类”
例如
public class SpecificationEvaluator<TEntity>
where TEntity : class
{
}
但这不是有点违背了创建特定约束的目的吗?
问题 - 是否有另一种解决方案我可以实现同时拥有“BaseEntity”和“用户” 作为约束?
【问题讨论】:
-
不,where contrainst 子句还不承认 OR 逻辑。
-
这能回答你的问题吗? Generic method multiple (OR) type constraint
标签: c# type-constraints generic-constraints