下記のRepositoryBaseから派生してRepositoryクラスを作成
_tableプロパティからLinqをかけば、
通常はLinq to Entityでクエリ構築、検索が行われ
SetTableData()に普通の配列などを渡せば、Linq to Objectで検索が行われるので
データベースに入ってる想定のデータを渡しててやれば、
実際にはデータベースに接続せずにRepositoryのテストを行うことが出来る。
また、DBに入っている値を入力としてテストが出来るので
Repositoryの検索関数をMockにして、検索関数が返す値を入力として扱う場合より広範囲を保護することが出来る。
public class RepositoryBase<TContext,TEntity> where TContext:DbContext,new() where TEntity : class { DbContext context; protected IEnumerable<TEntity> _tabledata; protected IQueryable<TEntity> _table { get { return _tabledata.AsQueryable(); } } protected IDbSet<TEntity> _dbset { get { return _tabledata as IDbSet<TEntity>; } } public void SetTableData(IEnumerable<TEntity> data) { _tabledata = data; } protected RepositoryBase() { context = new TContext(); SetTableData(context.Set<TEntity>()); } }