私は非常に単純な汎用リポジトリを持っています:
public interface IRepository<TEntity, TNotFound>
where TEntity : EntityObject
where TNotFound : TEntity, new()
{
IList<TEntity> GetAll();
TEntity With(int id);
TEntity Persist(TEntity itemToPersist);
void Delete(TEntity itemToDelete);
}
Term
特別な動作をせずに、そのタイプのリポジトリのコントラクトを定義したいと思います。したがって、次のようになります。
public class TermNotFound : Term
{ public TermNotFound() : base(String.Empty, String.Empty) { } }
public interface ITermRepository : IRepository<Term, TermNotFound> { }
ここでテストのために、汎用リポジトリのメモリ内実装を作成したいので、これを持っています(簡潔にするために終了していません):
public class InMemoryRepository<TEntity, TNotFound> : IRepository<TEntity, TNotFound>
where TEntity : EntityObject
where TNotFound : TEntity, new()
{
private IList<TEntity> _repo = new List<TEntity>();
public IList<TEntity> GetAll()
{
return this._repo;
}
public TEntity With(int id)
{
return this._repo.SingleOrDefault(i => i.Id == id) ?? new TNotFound();
}
public TEntity Persist(TEntity itemToPersist)
{
throw new NotImplementedException();
}
public void Delete(TEntity itemToDelete)
{
throw new NotImplementedException();
}
}
私がそれをどのように機能させたいかを理解するのは難しいことではありません。私のテストでは、ジェネリックInMemoryRepository
実装を注入して自分のを作成したいと思いITermRepository
ます。それはどれくらい難しいですか?
ええと、StructureMapにそれをさせることはできません。スキャナーでとを使用してみWithDefaultConventions
ましたが、成功しませんでした。ConnectImplementationsToTypesClosing(typeof(IRepository<,>))
誰かが私を助けてくれますか?