Skip to content

Instantly share code, notes, and snippets.

@brendankowitz
Last active August 29, 2015 13:59
Show Gist options
  • Save brendankowitz/10807118 to your computer and use it in GitHub Desktop.
Save brendankowitz/10807118 to your computer and use it in GitHub Desktop.
public class InMemoryDbContext : IDbContext
{
// ReSharper disable StaticFieldInGenericType
static int _lastId;
// ReSharper restore StaticFieldInGenericType
IList<object> _entities;
public InMemoryDbContext(params object[] preexistingEntities)
{
SetPreExistingEntities(preexistingEntities);
}
public void SetPreExistingEntities(params object[] preexistingEntities)
{
_entities = preexistingEntities.ToList();
Save();
}
public IDbSet<TEntity> Set<TEntity>() where TEntity : class
{
return new InMemoryEntitySet<TEntity>(_entities);
}
public void Save()
{
var newToSearch = new List<object>(_entities);
do
{
var currentSearch = newToSearch;
newToSearch = new List<object>();
foreach (var entity in currentSearch)
{
var idprop = entity.GetType().GetProperty("Id");
if (idprop != null && idprop.CanRead && idprop.CanWrite && (idprop.PropertyType == typeof(int) || idprop.PropertyType == typeof(long)))
{
var current = (long)Convert.ChangeType(idprop.GetValue(entity, null), typeof(long));
if (current == 0)
{
var id = Interlocked.Increment(ref _lastId);
idprop.SetValue(entity, id, null);
}
}
else if (idprop != null && idprop.CanRead && idprop.CanWrite && (idprop.PropertyType == typeof(string)))
{
var current = (string)Convert.ChangeType(idprop.GetValue(entity, null), typeof(string));
if (string.IsNullOrEmpty(current))
{
idprop.SetValue(entity, Guid.NewGuid().ToString(), null);
}
}
}
} while (newToSearch.Any());
}
public IDbTransaction BeginTransaction()
{
return NSubstitute.Substitute.For<IDbTransaction>();
}
public void Dispose()
{
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Tests
{
public class InMemoryEntitySet<TEntity> : IDbSet<TEntity> where TEntity : class
{
readonly IList<object> _entities;
private readonly IQueryable<TEntity> _asQueryable;
public InMemoryEntitySet(IList<object> entities)
{
_entities = entities;
_asQueryable = _entities.OfType<TEntity>().AsQueryable();
}
public IEnumerator<TEntity> GetEnumerator()
{
return _asQueryable.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public Expression Expression
{
get { return _asQueryable.Expression; }
}
public Type ElementType
{
get { return typeof(TEntity); }
}
public IQueryProvider Provider
{
get { return AsyncQueryProviderProvider; }
}
public TEntity Find(params object[] keyValues)
{
var idProperty = typeof(TEntity).GetProperty("Id", BindingFlags.Public | BindingFlags.Instance);
return _asQueryable
.FirstOrDefault(e => keyValues.Contains(idProperty.GetValue(e, null)));
}
public TEntity Add(TEntity entity)
{
_entities.Add(entity);
return entity;
}
public TEntity Remove(TEntity entity)
{
_entities.Remove(entity);
return entity;
}
public TEntity Attach(TEntity entity)
{
_entities.Add(entity);
return entity;
}
public TEntity Create()
{
return (TEntity)Activator.CreateInstance(typeof(TEntity));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment