Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework: Implement interfaces for unit test
    text
    copied!<p>today I wanted to start unit-testing a little asp.net MVC 3 Web (test) application to learn some new stuff.</p> <p>But things went worse then I had expected ...</p> <p>I've now read some threads about unit testing in relation with Entity framework and now I first want to implement interfaces for my entity framework related classes so that I can implement an in memory "database" for my unit tests.</p> <p>My code base is from <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application" rel="nofollow">ASP.NET MVC tutorial</a>. I've read <a href="http://msdn.microsoft.com/en-us/ff714955.aspx" rel="nofollow">MSDN</a> but it doesn't help me in my cases.</p> <p>I'd like to show you my code. I'm using a unit of work pattern with repositories:</p> <p>Unit of work:</p> <pre><code>public class SqlUnitOfWork : IUnitOfWork, IDisposable { private SqlContext context = new SqlContext(); private IGenericRepository&lt;Message&gt; messageRepository; private IGenericRepository&lt;Receipt&gt; receiptRepository; private IGenericRepository&lt;Useraccount&gt; useraccountRepository; private bool disposed = false; public IGenericRepository&lt;Message&gt; MessageRepository { get { if (this.messageRepository == null) { this.messageRepository = new SqlGenericRepository&lt;Message&gt;(context); } return messageRepository; } } public IGenericRepository&lt;Receipt&gt; ReceiptRepository { get { if (this.receiptRepository == null) { this.receiptRepository = new SqlGenericRepository&lt;Receipt&gt;(context); } return receiptRepository; } } public IGenericRepository&lt;Useraccount&gt; UseraccountRepository { get { if (this.useraccountRepository == null) { this.useraccountRepository = new SqlGenericRepository&lt;Useraccount&gt;(context); } return useraccountRepository; } } public SqlUnitOfWork() { } ~SqlUnitOfWork() { } public virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { context.Dispose(); } } this.disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public void Save() { context.SaveChanges(); } } </code></pre> <p>This one implements an interface I've created.</p> <p>My generic repository for sql:</p> <pre><code>public class SqlGenericRepository&lt;TEntity&gt; : IGenericRepository&lt;TEntity&gt; where TEntity : class { internal SqlContext context; internal DbSet&lt;TEntity&gt; dbSet; public SqlGenericRepository(SqlContext context) { this.context = context; this.dbSet = context.Set&lt;TEntity&gt;(); } ~SqlGenericRepository() { } public virtual IEnumerable&lt;TEntity&gt; Get(Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null, Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null, string includeProperties = "") { IQueryable&lt;TEntity&gt; query = dbSet; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } if (orderBy != null) { return orderBy(query).ToList(); } else { return query.ToList(); } } public virtual TEntity GetByID(object id) { return dbSet.Find(id); } public virtual void Insert(TEntity entity) { dbSet.Add(entity); } public virtual void Delete(object id) { TEntity entityToDelete = dbSet.Find(id); Delete(entityToDelete); } public virtual void Delete(TEntity entityToDelete) { if (context.Entry(entityToDelete).State == EntityState.Detached) { dbSet.Attach(entityToDelete); } dbSet.Remove(entityToDelete); } public virtual void Update(TEntity entityToUpdate) { dbSet.Attach(entityToUpdate); context.Entry(entityToUpdate).State = EntityState.Modified; } } </code></pre> <p>It implements an interface I've programmed:</p> <pre><code>public interface IGenericRepository&lt;TEntity&gt; where TEntity : class { IEnumerable&lt;TEntity&gt; Get(Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null, Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null, string includeProperties = ""); TEntity GetByID(object id); void Insert(TEntity entity); void Delete(object id); void Delete(TEntity entityToDelete); void Update(TEntity entityToUpdate); } </code></pre> <p>I would now like to implement an "InMemoryGenericRepository" for my unit tests, then an "InMemoryUnitOfWork". How would those "InMemoryGenericRepository" look like?</p> <p>I think I would use a generic List inside this repository where all data is stored:</p> <pre><code>IEnumerable&lt;TEntity&gt; List { get; set; } </code></pre> <p>But how can I adapt this method:</p> <pre><code>public virtual IEnumerable&lt;TEntity&gt; Get(Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null, Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null, string includeProperties = "") { IQueryable&lt;TEntity&gt; query = dbSet; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } if (orderBy != null) { return orderBy(query).ToList(); } else { return query.ToList(); } } </code></pre> <p>so it's working with my </p> <pre><code>IEnumerable&lt;TEntity&gt; List { get; set; } </code></pre> <p>I hope you made it until the end of my question.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload