Entity Framework 6, Repository pattern and Unit of Work

c# design-patterns entity-framework repository-pattern

Question

Entity Framework and several design patterns, including the repository pattern and the unit of work, are things I'm learning. A software I created makes advantage of these patterns. I only have one static DbContext object in this class, and I always utilize it. There is only one DbContext object present over the entire life of my application. Am I utilizing this method correctly? Consider how many people there might be. will there be any database connection issues?

UI coding

UnitOfWork uow = GetUnitOfWork(); //There is only one uow object
uow.EmployeeRepository.Insert(new Employee
{
    Name = name,
    Surname = surname
});

Code for GetUnitOfWork:

private static UnitOfWork _uow;
public static UnitOfWork GetUnitOfWork()
{
    return _uow ?? (_uow = new UnitOfWork());
}

Code for UnitOfWork:

public class UnitOfWork : IDisposable
{
    private static FollowerEntities _context;
    private DbContextTransaction _transaction;

    //repositories
    private EmployeeRepository _employeeRepository;


    public UnitOfWork()
    {
        _context = new FollowerEntities();
    }

    public EmployeeRepository EmployeeRepository
    {
        get { return _employeeRepository ?? (_employeeRepository = new EmployeeRepository(_context)); }
    }

    public void Save()
    {
        try
        {
            _transaction = _context.Database.BeginTransaction();
            _context.SaveChanges();
            _transaction.Commit();
        }
        catch
        {
            _transaction.Rollback();
            throw;
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (_context != null)
            {
                _context.Dispose();
                _context = null;
            }
        }
    }
}

Employee Database

public class EmployeeRepository : GenericRepository<Employee>
{
    public EmployeeRepository(FollowerEntities entities)
        : base(entities)
    {

    }
}

Common Repository

public class GenericRepository<T> where T : class
{
    private FollowerEntities _entities;


    private DbSet<T> table = null;

    public GenericRepository()
    {

    }

    public GenericRepository(FollowerEntities entities)
    {
        this._entities = entities;
        table = _entities.Set<T>();
    }

    public IEnumerable<T> SelectAll()
    {
        return table.ToList();
    }

    public T SelvectById(object id)
    {
        return table.Find(id);
    }

    public void Insert(T obj)
    {
        table.Add(obj);
    }

    public void Update(T obj)
    {
        table.Attach(obj);
        _entities.Entry(obj).State = EntityState.Modified;
    }

    public void Delete(object id)
    {
        T existing = table.Find(id);
        table.Remove(existing);
    }
}
1
2
11/17/2014 7:35:06 PM

Accepted Answer

ZZZ_tmp
4
11/17/2014 7:47:25 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow