public
Last active

Code for my post on Repository Pattern for Entity Framework: http://coderjournal.com/2010/11/entity-framework-repository-pattern/

  • Download Gist
IObjectContext.cs
C#
1 2 3 4 5 6 7 8 9
public interface IObjectContext : IDisposable
{
ObjectContextOptions ContextOptions { get; }
 
TEntity CreateObject<TEntity>() where TEntity : class;
IObjectSet<TEntity> CreateObjectSet<TEntity>() where TEntity : class;
 
int SaveChanges();
}
IRepository.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13
public interface IRepository<TEntity>
{
IQueryable<TEntity> All();
IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);
 
TEntity Create();
 
void Add(TEntity entity);
void Delete(TEntity entity);
 
void Attach(TEntity entity);
void Detach(TEntity entity);
}
IRepositorySession.cs
C#
1 2 3 4 5
public interface IRepositorySession : IDisposable
{
IObjectContext Container { get; }
void Commit();
}
Repository.cs
C#
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
public abstract class Repository<T> : IRepository<T>
where T : class
{
private IObjectContext _container;
private IObjectSet<T> _objectSet;
 
public Repository(IRepositorySession session)
{
Session = session;
_container = Session.Container;
}
 
protected IRepositorySession Session { get; private set; }
 
private IObjectSet<T> ObjectSet
{
get
{
if (_objectSet == null)
_objectSet = _container.CreateObjectSet<T>();
 
return _objectSet;
}
}
 
#region IRepository<T> Members
 
public IQueryable<T> All()
{
return ObjectSet;
}
 
public IQueryable<T> Where(Expression<Func<T, bool>> predicate)
{
return ObjectSet.Where(predicate);
}
 
public virtual T Create()
{
return _container.CreateObject<T>();
}
 
public virtual void Add(T entity)
{
ObjectSet.AddObject(entity);
}
 
public virtual void Delete(T entity)
{
ObjectSet.DeleteObject(entity);
}
 
public void Attach(T entity)
{
ObjectSet.Attach(entity);
}
 
public void Detach(T entity)
{
ObjectSet.Detach(entity);
}
 
#endregion
}
RepositoryHelper.cs
C#
1 2 3 4 5 6 7 8 9 10
public static class RepositoryHelper
{
public static IQueryable<T> Include<T>(this IQueryable<T> source, string path)
{
if (source is ObjectQuery<T>)
return ((ObjectQuery<T>)source).Include(path);
 
return source;
}
}
RepositorySession.cs
C#
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 111 112 113 114 115 116 117 118
public class RepositorySession : IRepositorySession
{
public static Type DefaultContainerType { get; set; }
 
[ThreadStatic]
private static RepositorySession _current;
 
public static RepositorySession Current
{
get { return _current; }
protected set { _current = value; }
}
 
private bool _disposed;
private IObjectContext _container;
 
public RepositorySession()
{
if (Current != null)
throw new ApplicationException("There is already an active session, a new one cannot be created.");
 
_container = Activator.CreateInstance(DefaultContainerType) as IObjectContext;
Current = this;
}
 
public RepositorySession(IObjectContext container)
{
_container = container;
}
 
public IObjectContext Container
{
get { return _container; }
}
 
public bool LazyLoadingEnabled
{
get
{
DisposedCheck();
return _container.ContextOptions.LazyLoadingEnabled;
}
set
{
DisposedCheck();
_container.ContextOptions.LazyLoadingEnabled = value;
}
}
 
public bool ProxyCreationEnabled
{
get
{
DisposedCheck();
return _container.ContextOptions.ProxyCreationEnabled;
}
set
{
DisposedCheck();
_container.ContextOptions.ProxyCreationEnabled = value;
}
}
 
#region ISession Members
 
public void Commit()
{
_container.SaveChanges();
}
 
#endregion
 
private void DisposedCheck()
{
if (_disposed)
throw new ObjectDisposedException(ToString());
}
 
#region IDisposable Members
 
/// <summary>
///
/// </summary>
public void Dispose()
{
Dispose(true);
}
 
/// <summary>
/// The dispose.
/// </summary>
/// <param name="disposing">
/// The disposing.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed && disposing)
{
if (_container != null)
_container.Dispose();
 
if (Current == this)
Current = null;
}
 
_disposed = true;
}
 
/// <summary>
/// Finalizes an instance of the <see cref="CassandraSession"/> class.
/// </summary>
~RepositorySession()
{
Dispose(false);
}
 
#endregion
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.