MVC は私の問題とは何の関係もありません。例に混乱しないでください。その純粋なC#の問題
質問のタイトルは、私が思う質問をうまく説明していません。
と呼ばれるいくつかのエンティティクラスの基本クラスがあるとしますEntityBase
一部のクラスは次のようになります
class Entity1 : EntityBase
class Entity2 : EntityBase
エンティティの基本操作で動作する抽象的なリポジトリがあります。宣言は次のとおりです。
abstract class RepositoryBase<TEntity> where TEntity : EntityBase
そして、このクラスにはいくつかの実装があります
class Repository1 : RepositoryBase<Entity1>
class Repository2 : RepositoryBase<Entity2>
現在、ベースを持つコントローラーがいくつかあります。
public abstract class RepositoryControllerBase<TRepository, TEntity>
where TRepository : RepositoryBase<TEntity>
where TEntity : EntityBase
そして、実装は次のようになります
class Controller1 : RepositoryControllerBase<Repository1, Entity1>
class Controller2 : RepositoryControllerBase<Repository2, Entity2>
Repository1
ここで、コントローラのリポジトリのタイプが である場合、エンティティ タイプは である必要があることに気付いたはずEntity1
です。それ以外の場合は、コンパイル エラーになります。
したがって、2 番目のジェネリック型をスキップして、それを自動的に推論する方法があると思います。方法がわかりません。助言がありますか?
?
もしかしたら、Java なら問題は簡単に解決できるかもしれません。ControllerBase 宣言を次のように置き換えます
public abstract class RepositoryControllerBase<TRepository>
where TRepository : RepositoryBase<?>