Skip to content

Instantly share code, notes, and snippets.

@dasjestyr
Created November 14, 2013 04:21
Show Gist options
  • Save dasjestyr/7461302 to your computer and use it in GitHub Desktop.
Save dasjestyr/7461302 to your computer and use it in GitHub Desktop.
Unit of Work example. Shows a single control class that will house a single context and all relevant repositories using the generic repository pattern
using System;
namespace mycompany.employee_manager.Core.Data.Repository
{
public class UnitOfWork : IDisposable
{
#region -- Fields --
private readonly MyContext _context = new MyContext();
private GenericRepository<User> _userRepository;
private GenericRepository<Department> _departmentRepository;
#endregion
#region -- Repositories --
public GenericRepository<User> UserRepository
{
get { return _userRepository ?? (_userRepository = new GenericRepository<User>(_context)); }
}
public GenericRepository<Department> DepartmentRepository
{
get
{
return _departmentRepository ??
(_departmentRepository = new GenericRepository<Department>(_context));
}
}
#endregion
#region -- IDisposable --
private bool _disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_context.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