如何重用泛型接口?

How to reuse generic interface?

我在 ASP.NET 核心上使用 ASP.NET 样板。 我有一些型号 classes.

public class AClass : FullAuditedEntity<int>
{

}

和一个如下所示的界面。

public interface ISomeInterface<T, TEntity> where T : BaseFileEntity where TEntity : class, IEntity<int>
{

}

现在,如果我添加一个额外的模型 class,如下所示。

public class BClass : FullAuditedEntity<string>
{

}

我必须为此更改定义另一个接口,如下所示。

public interface ISomeInterface<T, TEntity> where T : BaseFileEntity where TEntity : class, IEntity<string>
{

}

基本上是重复代码。有没有更好的方法来做到这一点?

您可以将界面更改为:

public interface ISomeInterface<T, TEntity, U> where T : BaseFileEntity where TEntity : class, IEntity<U>
{

}

是否可以像下面这样声明您的接口,以便您可以将类型传递给 IEntity

public interface ISomeInterfaceB<T, TEntity, TK> where T : BaseFileEntity where TEntity : class, IEntity<TK> {

    }