Skip to content

Instantly share code, notes, and snippets.

@JordyLangen
Last active March 21, 2018 05:18
Show Gist options
  • Save JordyLangen/7801950 to your computer and use it in GitHub Desktop.
Save JordyLangen/7801950 to your computer and use it in GitHub Desktop.
Simple IUnitOfWork for Entity Framework implementation without using repositories.
public interface IUnitOfWork : IDisposable
{
/// <summary>
/// Gets this instance.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
IQueryable<T> All<T>() where T : class;
/// <summary>
/// Finds the specified keys.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="keys">The keys.</param>
/// <returns></returns>
T Find<T>(params object[] keys) where T : class;
/// <summary>
/// Adds the specified entry.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entry">The entry.</param>
/// <returns></returns>
T Add<T>(T entry) where T : class;
/// <summary>
/// Removes the specified entry.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entry">The entry.</param>
/// <returns></returns>
T Remove<T>(T entry) where T : class;
/// <summary>
/// Attaches the specified entry.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entry">The entry.</param>
/// <returns></returns>
T Attach<T>(T entry) where T : class;
/// <summary>
/// Saves the changes.
/// </summary>
void SaveChanges();
}
public class UnitOfWork : DbContext, IUnitOfWork
{
...
/// <summary>
/// Indicates if this instance is disposed
/// </summary>
private bool _disposed;
/// <summary>
/// Gets this instance.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IQueryable<T> All<T>() where T : class
{
return Set<T>();
}
/// <summary>
/// Finds the specified keys.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="keys">The keys.</param>
/// <returns></returns>
public T Find<T>(params object[] keys) where T : class
{
return Set<T>().Find(keys);
}
/// <summary>
/// Adds the specified entry.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entry">The entry.</param>
/// <returns></returns>
public T Add<T>(T entry) where T : class
{
return Set<T>().Add(entry);
}
/// <summary>
/// Removes the specified entry.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entry">The entry.</param>
/// <returns></returns>
public T Remove<T>(T entry) where T : class
{
return Set<T>().Remove(entry);
}
/// <summary>
/// Attaches the specified entry.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entry">The entry.</param>
/// <returns></returns>
public T Attach<T>(T entry) where T : class
{
return Set<T>().Attach(entry);
}
/// <summary>
/// Saves the changes.
/// </summary>
public new void SaveChanges()
{
base.SaveChanges();
}
/// <summary>
/// Finalizes an instance of the <see cref="IntegraleKlantrapportageContext" /> class.
/// </summary>
~UnitOfWork()
{
Dispose(false);
}
/// <summary>
/// Disposes the context. The underlying <see cref="T:System.Data.Objects.ObjectContext" /> is also disposed if it was created
/// is by this context or ownership was passed to this context when this context was created.
/// The connection to the database (<see cref="T:System.Data.Common.DbConnection" /> object) is also disposed if it was created
/// is by this context or ownership was passed to this context when this context was created.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
if (!_disposed)
{
base.Dispose(disposing);
_disposed = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment