Created
April 11, 2011 15:04
-
-
Save troufster/913659 to your computer and use it in GitHub Desktop.
Entity Framework 4.1 DbSet Mock from http://msdn.microsoft.com/en-us/ff714955.aspx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InMemoryDbSet<T> : IDbSet<T> where T : class | |
{ | |
readonly HashSet<T> _set; | |
readonly IQueryable<T> _queryableSet; | |
public InMemoryDbSet() : this(Enumerable.Empty<T>()) { } | |
public InMemoryDbSet(IEnumerable<T> entities) { | |
_set = new HashSet<T>(); | |
foreach (var entity in entities) { | |
_set.Add(entity); | |
} | |
_queryableSet = _set.AsQueryable(); | |
} | |
public T Add(T entity) | |
{ | |
_set.Add(entity); | |
return entity; | |
} | |
public T Attach(T entity) | |
{ | |
_set.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 System.Collections.ObjectModel.ObservableCollection<T> Local | |
{ | |
get { throw new NotImplementedException(); } | |
} | |
public T Remove(T entity) | |
{ | |
_set.Remove(entity); | |
return entity; | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return _set.GetEnumerator(); | |
} | |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
public Type ElementType | |
{ | |
get { return _queryableSet.ElementType; } | |
} | |
public System.Linq.Expressions.Expression Expression | |
{ | |
get { return _queryableSet.Expression; } | |
} | |
public IQueryProvider Provider | |
{ | |
get { return _queryableSet.Provider; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good stuff. Good mock for an IDbSet . Thanks!