Created
October 24, 2011 16:31
-
-
Save LukeWinikates/1309447 to your computer and use it in GitHub Desktop.
An implementation of IDbSet to help with mocking Entity Framework DbContexts.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace EntityExtensions { | |
public class FakeDbSet<T> : System.Data.Entity.IDbSet<T> where T : class { | |
private readonly List<T> list = new List<T>(); | |
public FakeDbSet() { | |
list = new List<T>(); | |
} | |
public FakeDbSet(IEnumerable<T> contents) { | |
this.list = contents.ToList(); | |
} | |
#region IDbSet<T> Members | |
public T Add(T entity) { | |
this.list.Add(entity); | |
return entity; | |
} | |
public T Attach(T entity) { | |
this.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 System.Collections.ObjectModel.ObservableCollection<T> Local { | |
get { | |
throw new NotImplementedException(); | |
} | |
} | |
public T Remove(T entity) { | |
this.list.Remove(entity); | |
return entity; | |
} | |
#endregion | |
#region IEnumerable<T> Members | |
public IEnumerator<T> GetEnumerator() { | |
return this.list.GetEnumerator(); | |
} | |
#endregion | |
#region IEnumerable Members | |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { | |
return this.list.GetEnumerator(); | |
} | |
#endregion | |
#region IQueryable Members | |
public Type ElementType { | |
get { return this.list.AsQueryable().ElementType; } | |
} | |
public System.Linq.Expressions.Expression Expression { | |
get { return this.list.AsQueryable().Expression; } | |
} | |
public IQueryProvider Provider { | |
get { return this.list.AsQueryable().Provider; } | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very good. Thanks.