から単体テスト、stackoverflowの中にここにリンクされ、フォーム他の回答リポジトリパターンと、私は非常にシンプルで直感的なリポジトリパターンのいくつかの実装を見てきましたメモリ
http://www.codeproject.com/Tips/309753/Repository-Pattern-with-Entity-Framework-4-1-and-C http://www.remondo.net/repository-pattern-example-csharp/
public interface IRepository<T>
{
void Insert(T entity);
void Delete(T entity);
IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
IQueryable<T> GetAll();
T GetById(int id);
}
public class Repository<T> : IRepository<T> where T : class, IEntity
{
protected Table<T> DataTable;
public Repository(DataContext dataContext)
{
DataTable = dataContext.GetTable<T>();
}
...
がどのように設定することができます単体テストを行うときにメモリから作業するのですか?メモリ内の何かからDataContextまたはLinqテーブルを構築する方法はありますか?私の考えは、コレクション(List、Dictionary ...)を作成し、単体テスト時にスタブすることでした。
ありがとうございます!
EDIT: 私はこのような何か必要なもの:
:- を私はクラスBook
- を持って、私は
Library
コンストラクタでクラスライブラリ を持って、私はリポジトリを初期化します
var bookRepository = new Repository<Book>(dataContext)
テストする際
Library
方法は、私は、メモリコンテキストを提供したい、このpublic Book GetByID(int bookID) { return bookRepository.GetByID(bookID) }
のように、リポジトリを使用しています。プロダクションでは、実際のデータベースのコンテキストを提供します。
私はあなたの要求、次のいくつかのサンプルコードを追加しました。 –