Skip to content

Instantly share code, notes, and snippets.

@jonfazzaro
Last active August 22, 2016 16:46
Show Gist options
  • Save jonfazzaro/be919f9064ce8d135f02c49ddb0c1bd0 to your computer and use it in GitHub Desktop.
Save jonfazzaro/be919f9064ce8d135f02c49ddb0c1bd0 to your computer and use it in GitHub Desktop.
In-memory implementation of IDbSet, for unit testing.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
public class MemoryDbSet<T> : IDbSet<T> where T : class {
readonly List<T> _list = new List<T>();
public MemoryDbSet() {
_list = new List<T>();
}
public MemoryDbSet(IEnumerable<T> contents) {
_list = contents.ToList();
}
public T Add(T entity) {
_list.Add(entity);
return entity;
}
public T Attach(T entity) {
_list.Add(entity);
return entity;
}
public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T {
throw new NotImplementedException();
}
public T Create() {
throw new NotImplementedException();
}
public T Find(params object[] keyValues) {
throw new NotImplementedException();
}
public ObservableCollection<T> Local {
get {
throw new NotImplementedException();
}
}
public T Remove(T entity) {
_list.Remove(entity);
return entity;
}
public IEnumerator<T> GetEnumerator() {
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return _list.GetEnumerator();
}
public Type ElementType {
get { return _list.AsQueryable().ElementType; }
}
public Expression Expression {
get { return _list.AsQueryable().Expression; }
}
public IQueryProvider Provider {
get { return _list.AsQueryable().Provider; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment