Created
November 14, 2013 04:21
-
-
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
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
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