-
-
Save MikaelEliasson/783ddf75f06be5a29a9d to your computer and use it in GitHub Desktop.
Suggestion for generic fake IDbSet for EF. Update to handle new entities
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
internal static class FakeDbSetBuilder | |
{ | |
public static FakeDbSetBuilder<T> New<T>() where T : class | |
{ | |
return new FakeDbSetBuilder<T>(); | |
} | |
} | |
internal class FakeDbSetBuilder<T> where T : class | |
{ | |
public GenericFakeDbSet<T, TKey> Build<TKey>(Func<T, TKey> keySelector) | |
{ | |
return new GenericFakeDbSet<T, TKey>(keySelector); | |
} | |
} |
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
internal class GenericFakeDbSet<T, TKey> : IDbSet<T> where T : class | |
{ | |
readonly Func<T, TKey> _keySelector; | |
readonly HashSet<T> _hash; | |
readonly IQueryable _query; | |
public GenericFakeDbSet(Func<T, TKey> keySelector) | |
{ | |
_hash = new HashSet<T>(); | |
_query = _hash.AsQueryable(); | |
_keySelector = keySelector; | |
} | |
public T Find(params object[] keyValues) | |
{ | |
var value = keyValues.First(); | |
return this.SingleOrDefault(item => _keySelector(item).Equals(value)); | |
} | |
public void RemoveMatch(T entity) | |
{ | |
if (_keySelector(entity).Equals(default(TKey))) | |
{ | |
return; | |
} | |
this.Remove(Find(_keySelector(entity))); | |
} | |
public T Add(T entity) | |
{ | |
RemoveMatch(entity); | |
_hash.Add(entity); | |
return entity; | |
} | |
public T Attach(T entity) | |
{ | |
RemoveMatch(entity); | |
_hash.Add(entity); | |
return entity; | |
} | |
public TDerivedEntity Create<TDerivedEntity>() | |
where TDerivedEntity : class, T | |
{ | |
return typeof(TDerivedEntity) as TDerivedEntity; | |
} | |
public T Create() | |
{ | |
return typeof(T) as T; | |
} | |
public ObservableCollection<T> Local | |
{ | |
get | |
{ | |
throw new NotImplementedException("Must override Local"); | |
} | |
} | |
public T Remove(T entity) | |
{ | |
_hash.Remove(entity); | |
return entity; | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return _hash.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return _hash.GetEnumerator(); | |
} | |
public Type ElementType | |
{ | |
get { return _query.ElementType; } | |
} | |
public Expression Expression | |
{ | |
get { return _query.Expression; } | |
} | |
public IQueryProvider Provider | |
{ | |
get { return _query.Provider; } | |
} | |
} |
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
[TestClass] | |
public class GenericFakeDbSetTest | |
{ | |
[TestMethod] | |
public void FindTest() | |
{ | |
var fake = FakeDbSetBuilder.New<Person>().Build(p => p.ID); | |
fake.Add(new Person { ID = 1, Name = "Alice" }); | |
fake.Add(new Person { ID = 2, Name = "Bob" }); | |
var result = fake.Find(2); | |
Assert.AreEqual("Bob", result.Name); | |
} | |
[TestMethod] | |
public void AddTest() | |
{ | |
var fake = FakeDbSetBuilder.New<Person>().Build(p => p.ID); | |
fake.Add(new Person { Name = "Alice" }); | |
fake.Add(new Person { Name = "Bob" }); | |
var result = fake.Count(); | |
Assert.AreEqual(2, result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment