4
\$\begingroup\$

I am trying to work on a WPF application and decided to go for Unit of Work and Repository pattern, along with MVVM. I am using Entity Framework as my ORM. Could you please do a code review?

IGenericRepository.cs

public interface IGenericRepository<T>
{
    T GetById(int id);
    IQueryable<T> GetAll();
    void Edit(T entity);
    void Insert(T entity);
    void Delete(T entity);
}

IUnitOfWork.cs

public interface IUnitOfWork : IDisposable
{
    GenericRepository<T_Asset> AssetRepository { get; }
    GenericRepository<T_ColumnFormula> FormulaRepository { get; }
    GenericRepository<T_Column> ColumnRepository { get; }
    void Save();
}

GenericRepository.cs

    public class GenericRepository<T> : IGenericRepository<T> where T : class
    {
        protected DbContext _objDataContext;
        protected readonly DbSet<T> _objDbSet; 

        public GenericRepository(DbContext objDataContext)
        {
            _objDataContext = objDataContext;
            _objDbSet = objDataContext.Set<T>();
        }

        public T GetById(int id)
        {
            return _objDbSet.Find(id);
        }

        public IQueryable<T> GetAll()
        {
            return _objDbSet;
        }

        public void Insert(T entity)
        {
            _objDbSet.Add(entity);
        }

        public void Edit(T entity)
        {
            _objDataContext.Entry(entity).State = EntityState.Modified;
        }

        public void Delete(T entity)
        {
            _objDataContext.Entry(entity).State = EntityState.Deleted;
        }
    }

UnitOfWork.cs

public class UnitOfWork : IUnitOfWork
{
    private GenericRepository<T_Asset> _assetRepository;
    private GenericRepository<T_ColumnFormula> _formulaRepository;
    private GenericRepository<T_Columns> _columnRepository;
    private DbContext _objDataContext;

    public UnitOfWork(string connectionString)
    {
        _objDataContext = new FormulaBuilderContext(connectionString);
    }

    public GenericRepository<T_Asset> AssetRepository
    {
        get
        {
            if (_assetRepository == null)
            {
                _assetRepository = new GenericRepository<T_Asset>(_objDataContext);
            }
            return _assetRepository;
        }
    }

    public GenericRepository<T_ColumnFormula> FormulaRepository
    {
        get
        {
            if (_formulaRepository == null)
            {
                _formulaRepository = new GenericRepository<T_ColumnFormula>(_objDataContext);
            }
            return _formulaRepository;
        }
    }

    public GenericRepository<T_Column> ColumnRepository
    {
        get
        {
            if (_columnRepository == null)
            {
                _columnRepository = new GenericRepository<T_Column>(_objDataContext);
            }
            return _columnRepository;
        }
    }

    public void Save()
    {
        _objDataContext.SaveChanges();
    }

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

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (_objDataContext != null)
            {
                _objDataContext.Dispose();
            }
        }
    }
}

Please suggest necessary changes in order to implement the patterns properly. Also I am ready to share more information if necessary.

\$\endgroup\$
  • 3
    \$\begingroup\$ Exactly what problem are you solving with that generic UoW+repository pattern, that the DbContext isn't already solving? Including your DbContext class could help us help you, and do you actually have classes named T_Column, T_ColumnFormula and T_Asset? \$\endgroup\$ – Mathieu Guindon Oct 21 '15 at 19:33
  • \$\begingroup\$ These are the tables in my system, since I am using entity the model classes have the same namr \$\endgroup\$ – Ishan070692 Oct 22 '15 at 4:24
  • 6
    \$\begingroup\$ This is yet another wrapper around basic DbContext/DbSet classes. Most people discover sooner or later that you gain nothing by this pattern. It's an illusion to think that you can abstract away any ORM implementation. \$\endgroup\$ – Gert Arnold Jan 21 '16 at 20:37
  • \$\begingroup\$ More specifically, I don't understand why you confine the UoW to three types. What is the rationale behind this? Please respond if you're still waiting for an answer. \$\endgroup\$ – Gert Arnold Jan 21 '16 at 20:39
  • \$\begingroup\$ One of the things "respository over an ORM" can provide is to avoid exposing things you might not want to let clients do. But in this case, it isn't hidding to many things. \$\endgroup\$ – bruno.bologna Apr 20 '17 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.