Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View EntityContext.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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
}
View EntityContext.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
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
}
View EntityContext.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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;
}
View EntityContext.cs
1 2 3 4 5 6 7 8 9 10
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);
}
View EntityContext.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
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);
}
}
}
}
}
View EntityContext.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
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.