-
-
Save sippylabs/4ada4d2f3a214e79ee78 to your computer and use it in GitHub Desktop.
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
public class Context : IContext | |
{ | |
public readonly ServiceEntities _entities; | |
#region Constructor | |
public Context() | |
{ | |
_entities = new ServiceEntities(); | |
} | |
#endregion | |
#region Implementation of IContext | |
public IMutableQuery<Category> Categories | |
{ | |
get { return new MutableQueryable<Category>(_entities, _entities.Categories); } | |
} | |
public IMutableQuery<User> Users | |
{ | |
get { return new MutableQueryable<User>(_entities, _entities.Users); } | |
} | |
public int SaveChanges() | |
{ | |
return _entities.SaveChanges(); | |
} | |
#endregion | |
} |
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
public interface IContext | |
{ | |
IMutableQuery<Category> Categories { get; } | |
IMutableQuery<User> Users { get; } | |
int SaveChanges(); | |
} |
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
public interface IMutableQuery<T> : IQueryable<T> | |
{ | |
T Create(T item); | |
T Delete(T item); | |
T Update(T item); | |
} |
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
public class MutableQueryable<T> : IMutableQuery<T> where T : class | |
{ | |
private readonly ServiceEntities _entities; | |
private readonly IQueryable<T> _queryable; | |
#region Constructor | |
public MutableQueryable(ServiceEntities entities, IQueryable<T> queryable) | |
{ | |
_entities = entities; | |
_queryable = queryable; | |
} | |
#endregion | |
#region Implementation of IEnumerable | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return _queryable.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
#endregion | |
#region Implementation of IMutableQuery<T> | |
public T Create(T item) | |
{ | |
var objectSet = _entities.CreateObjectSet<T>(); | |
objectSet.AddObject(item); | |
return item; | |
} | |
public T Delete(T item) | |
{ | |
var objectSet = _entities.CreateObjectSet<T>(); | |
objectSet.DeleteObject(item); | |
return item; | |
} | |
public T Update(T item) | |
{ | |
_entities.TryAddObject(item); | |
_entities.ObjectStateManager.ChangeObjectState(item, EntityState.Modified); | |
return item; | |
} | |
#endregion | |
#region Implementation of IQueryable | |
public Expression Expression | |
{ | |
get { return _queryable.Expression; } | |
} | |
public Type ElementType | |
{ | |
get { return _queryable.ElementType; } | |
} | |
public IQueryProvider Provider | |
{ | |
get { return _queryable.Provider; } | |
} | |
#endregion | |
} |
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
public static class ObjectContextExtensions | |
{ | |
public static bool TryAddObject<T>(this ObjectContext objectContext, T item) where T : class | |
{ | |
Contract.Requires<ArgumentNullException>(objectContext != null); | |
Contract.Requires<ArgumentNullException>(item != null); | |
try | |
{ | |
objectContext.CreateObjectSet<T>().AddObject(item); | |
return true; | |
} | |
catch (InvalidOperationException) | |
{ | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment