Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
public class EntityContext : IContext
{
public const String DefaultConnectionString = "DefaultConnectionString";
private readonly string nameOrConnectionString;
private DbContext context;
public EntityContext(String nameOrConnectionString)
{
this.nameOrConnectionString = nameOrConnectionString;
if (String.IsNullOrEmpty(nameOrConnectionString))
{
this.nameOrConnectionString = DefaultConnectionString;
}
}
#region Implementation of IDisposable
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(Boolean disposing)
{
if (!disposing) return;
if (context != null)
{
context.Dispose();
}
}
#endregion
#region Implementation of IContext
public virtual void Initialize()
{
if (context != null) return;
if (ConfigurationManager.ConnectionStrings.Count == 0)
{
throw new InvalidDataException("No connection strings found in application configuration file.");
}
context = new DbContext(nameOrConnectionString);
context.Configuration.LazyLoadingEnabled = true;
context.Configuration.ProxyCreationEnabled = true;
}
public virtual void MarkDeleted<T>(T item) where T : class
{
context.Entry(item).State = EntityState.Deleted;
}
public virtual void MarkDirty<T>(T item) where T : class
{
context.Entry(item).State = EntityState.Modified;
}
public virtual void MarkNew<T>(T item) where T : class
{
context.Entry(item).State = EntityState.Added;
}
public virtual void Save()
{
context.SaveChanges();
}
public virtual IRepository<T> Set<T>() where T : class
{
return context.Set<T>().ToRepository();
}
#endregion
}
public class EntityRepository<T> : IRepository<T> where T : class
{
private readonly DbSet<T> set;
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Object" /> class.
/// </summary>
public EntityRepository(DbSet<T> set)
{
this.set = set;
}
#region Implementation of IEnumerable
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.
/// </returns>
public IEnumerator<T> GetEnumerator()
{
return set.AsEnumerable().GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region Implementation of IQueryable
/// <summary>
/// Gets the predicate tree that is associated with the instance of <see cref="T:System.Linq.IQueryable" />.
/// </summary>
/// <returns>
/// The <see cref="T:System.Linq.Expressions.Expression" /> that is associated with this instance of
/// <see cref="T:System.Linq.IQueryable" />.
/// </returns>
public Expression Expression
{
get { return set.AsQueryable().Expression; }
}
/// <summary>
/// Gets the type of the element(s) that are returned when the predicate tree associated with this instance of
/// <see cref="T:System.Linq.IQueryable" /> is executed.
/// </summary>
/// <returns>
/// A <see cref="T:System.Type" /> that represents the type of the element(s) that are returned when the predicate
/// tree associated with this object is executed.
/// </returns>
public Type ElementType
{
get { return set.AsQueryable().ElementType; }
}
/// <summary>
/// Gets the query provider that is associated with this data source.
/// </summary>
/// <returns>
/// The <see cref="T:System.Linq.IQueryProvider" /> that is associated with this data source.
/// </returns>
public IQueryProvider Provider
{
get { return set.AsQueryable().Provider; }
}
#endregion
#region Implementation of IRepository<T>
public T Attach(T item)
{
return set.Attach(item);
}
public IEnumerable<T> Find(Expression<Func<T, Boolean>> predicate)
{
return set.Where(predicate);
}
public IEnumerable<T> FindAll()
{
return set;
}
public Boolean Remove(T item)
{
return set.Remove(item) == item;
}
#endregion
}
public interface IContext : IDisposable
{
void Initialize();
void MarkDeleted<T>(T item) where T : class;
void MarkDirty<T>(T item) where T : class;
void MarkNew<T>(T item) where T : class;
void Save();
IRepository<T> Set<T>() where T : class;
}
public interface IRepository<T> : IQueryable<T>
{
T Attach(T item);
IEnumerable<T> Find(Expression<Func<T, Boolean>> predicate);
IEnumerable<T> FindAll();
Boolean Remove(T item);
}
public class TestDbContext : IContext
{
private readonly Dictionary<Type, List<Object>> objects = new Dictionary<Type, List<Object>>();
#region Implementation of IDisposable
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
GC.SuppressFinalize(this);
}
#endregion
#region Implementation of IContext
public void Initialize()
{
}
public void MarkDeleted<T>(T item) where T : class, IDbIdentity
{
}
public void MarkModified<T>(T item) where T : class, IDbIdentity
{
}
public void MarkNew<T>(T item) where T : class, IDbIdentity
{
}
public void Save()
{
}
public IRepository<T> Set<T>() where T : class, IDbIdentity
{
List<Object> collection;
if (!objects.TryGetValue(typeof (T), out collection))
{
collection = new List<Object>();
objects.Add(typeof (T), collection);
}
return new TestDbRepository<T>(collection.Cast<T>());
}
#endregion
public void AddEntity<T>(int count, object properties = null) where T : IDbIdentity, new()
{
if (!objects.ContainsKey(typeof (T)))
{
objects.Add(typeof (T), new List<Object>());
}
for (int i = 0; i < count; ++i)
{
T item = (T)Activator.CreateInstance(typeof (T));
item.Id = i;
if (properties != null)
{
foreach (var pair in GetProperties(properties))
{
System.Reflection.PropertyInfo prop =
item.GetType().
GetProperties().
SingleOrDefault(x => x.Name.Equals(pair.Key, StringComparison.OrdinalIgnoreCase) && x.CanWrite);
if (prop != null)
{
prop.SetValue(item, pair.Value);
}
}
}
objects[typeof (T)].Add(item);
}
}
private static IEnumerable<KeyValuePair<String, Object>> GetProperties(object o)
{
if (o != null)
{
var props = TypeDescriptor.GetProperties(o);
foreach (PropertyDescriptor prop in props)
{
object val = prop.GetValue(o);
if (val != null)
{
yield return new KeyValuePair<string, object>(prop.Name, val);
}
}
}
}
}
public class TestDbRepository<T> : IRepository<T> where T : class, IDbIdentity
{
private readonly List<T> list;
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Object"/> class.
/// </summary>
public TestDbRepository(IEnumerable<T> collection)
{
list = new List<T>(collection);
}
#region Implementation of IEnumerable
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
public IEnumerator<T> GetEnumerator()
{
return list.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region Implementation of IQueryable
/// <summary>
/// Gets the expression tree that is associated with the instance of <see cref="T:System.Linq.IQueryable"/>.
/// </summary>
/// <returns>
/// The <see cref="T:System.Linq.Expressions.Expression"/> that is associated with this instance of <see cref="T:System.Linq.IQueryable"/>.
/// </returns>
public Expression Expression
{
get { return list.AsQueryable().Expression; }
}
/// <summary>
/// Gets the type of the element(s) that are returned when the expression tree associated with this instance of <see cref="T:System.Linq.IQueryable"/> is executed.
/// </summary>
/// <returns>
/// A <see cref="T:System.Type"/> that represents the type of the element(s) that are returned when the expression tree associated with this object is executed.
/// </returns>
public Type ElementType
{
get { return list.AsQueryable().ElementType; }
}
/// <summary>
/// Gets the query provider that is associated with this data source.
/// </summary>
/// <returns>
/// The <see cref="T:System.Linq.IQueryProvider"/> that is associated with this data source.
/// </returns>
public IQueryProvider Provider
{
get { return list.AsQueryable().Provider; }
}
#endregion
#region Implementation of IRepository<T>
public T Attach(T item)
{
if (list.Any(x => ReferenceEquals(x, item) || x.Id == item.Id))
{
return null;
}
list.Add(item);
return item;
}
public IEnumerable<T> Find(Func<T, Boolean> predicate)
{
return list.Where(predicate);
}
public IEnumerable<T> Find(Expression<Func<T, Boolean>> predicate)
{
return list.AsQueryable().Where(predicate);
}
public IEnumerable<T> FindAll()
{
return list.AsReadOnly();
}
public bool Remove(T item)
{
return list.Remove(item);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.