Skip to content

Instantly share code, notes, and snippets.

@troufster
Created April 15, 2011 08:08
Show Gist options
  • Save troufster/921357 to your computer and use it in GitHub Desktop.
Save troufster/921357 to your computer and use it in GitHub Desktop.
DbContext abstraction
using System;
using System.Data.Entity;
using Hurf.Durf.Model;
using System.Data;
namespace Derp.Model.UnitOfWork
{
/// <summary>
/// Implements an abstraction of Entity Framework's
/// DbContext
/// </summary>
class EfContext : IContext
{
#region private properties
/// <summary>
/// Internal EF-typed context
/// </summary>
private DbContext _internalDbContext;
/// <summary>
/// Internal IContext mocked context
/// </summary>
private IContext _mockContext;
/// <summary>
/// Returns whether this instance has a "real" context or not
/// </summary>
private bool HasDbContext
{
get {
return _internalDbContext != null;
}
}
#endregion
#region IContext members
/// <summary>
/// Sets the internal EF-typed context
/// </summary>
/// <param name="context">The context to set</param>
public void SetContext(DbContext context) {
_internalDbContext = context;
}
/// <summary>
/// Sets the internal IContext mock context
/// </summary>
/// <param name="mockcontext">Context to set</param>
public void SetContext(IContext mockcontext) {
_mockContext = mockcontext;
}
/// <summary>
/// Saves the changes in the internal context
/// </summary>
/// <returns>Status</returns>
public int SaveChanges()
{
try
{
return HasDbContext ? _internalDbContext.SaveChanges() : _mockContext.SaveChanges();
}
catch (Exception ex)
{
throw new Exception("Could not save IContext changes", ex);
}
}
/// <summary>
/// Enables/Disables lazy loading in the internal context
/// </summary>
public bool LazyLoadingEnabled
{
get
{
return HasDbContext ? _internalDbContext.Configuration.LazyLoadingEnabled : _mockContext.LazyLoadingEnabled;
}
set
{
if (HasDbContext) {
_internalDbContext.Configuration.LazyLoadingEnabled = value;
return;
}
_mockContext.LazyLoadingEnabled = value;
}
}
/// <summary>
/// Gets/Sets the connection string of the internal context
/// </summary>
public string ConnectionString
{
get
{
return HasDbContext ? _internalDbContext.Database.Connection.ConnectionString : _mockContext.ConnectionString;
}
set
{
if (HasDbContext) {
_internalDbContext.Database.Connection.ConnectionString = value;
return;
}
_mockContext.ConnectionString = value;
}
}
/// <summary>
/// Get the internal contexts typed set of entities
/// </summary>
/// <typeparam name="T">Type of model</typeparam>
/// <returns>Set of entities</returns>
public IDbSet<T> Set<T>() where T : class
{
return HasDbContext ? _internalDbContext.Set<T>() : _mockContext.Set<T>();
}
/// <summary>
/// Gets the state of an entity within the internal context
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public EntityState GetEntityState(object entity)
{
return HasDbContext ? _internalDbContext.Entry(entity).State : EntityState.Unchanged;
}
/// <summary>
/// Sets the state of an entity within the internal context
/// </summary>
/// <param name="entity"></param>
/// <param name="state"></param>
public void SetEntityState(object entity, EntityState state)
{
if (HasDbContext)
{
_internalDbContext.Entry(entity).State = state;
}
}
#endregion
#region IDisposable members
private bool _disposed;
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing && HasDbContext) _internalDbContext.Dispose();
_disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment