Entity Framework DDD Generic Infrarepository

Asked

Viewed 1,056 times

0

How to do JOIN via lambda in the generic repository. I’m following the DDD architecture model.

Example:

 public class BaseRepository<T> : IRepositoryBase<T> where T : class
{
    private IDbContext _context;
    private readonly IDbSet<T> _dbset;

    public BaseRepository()
    {
        var contextManager = ServiceLocator.Current.GetInstance<IContextManager>() as ContextManager;
        _context = contextManager.AuditorExternoContext;
        _dbset = _context.Set<T>();
    }
    public virtual void Add(T entity)
    {
        _dbset.Add(entity);
    }

    public virtual void Update(T entity)
    {
        var entry = _context.Entry(entity);
        _dbset.Attach(entity);
        entry.State = EntityState.Modified;
    }

    public virtual void Delete(T entity)
    {
        var entry = _context.Entry(entity);
        entry.State = EntityState.Deleted;
    }

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

    public virtual IEnumerable<T> All()
    {
        return _dbset.ToList();
    }

    public virtual IQueryable<T> AllQueryable()
    {
        return _dbset.AsQueryable();
    }

    public virtual IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
    {
        return _dbset.Where(predicate);
    }

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

    protected virtual void Dispose(bool disposing)
    {
        if (!disposing) return;

        if (_context == null) return;
        _context.Dispose();
        _context = null;
    }

How I do Join I’m in a specific class?

public class FaturamentoRepository : BaseRepository<Faturamento>, IAnotacaoRepository
{
}

As I am wearing BaseRepository the FaturamentoRepository?

How to put the inside faturamentoReposity.

  • You cannot access _dbSet directly and do the query you need?

1 answer

3


Entity Framework already implements the Repository Standard

I’ve answered that a few times, as in this answer about Unit of Work and repository, but I’ll write another answer because for some reason the answer is hard to locate.

I’ll start with a question taken from Programmers. It is not the answer accepted, but it is the most voted, and it is the one that I consider correct. Here it is said that the DbContext already implements a Work Unit (Unit of Work). The Work Unit is the object responsible for implementing the abstraction between your database and the business layer of your application. It prevents you from having to write SQL sentences in hand to your repository.

The repository pattern was necessary at a time when object-relational mappers did not yet exist or were not well developed, which is not the case with the Entity Framework, where a large part of all bank operations have already been implemented. Among the functions of a repository are:

  • Bring records from the database already serialized as objects;
  • Persist objects as records from a database.

All this Entity Framework already does, which makes completely unnecessary implement one more layer just to deal with it. In addition to unnecessarily increasing the logic and complexity of your system, the gain with it is zero.

But I read in several places that to write tests I need a repository.

Yes, because literature has not yet been completely renewed. In this text there is an explanation of how to implement unit tests using a Mock of a DbContext. I use this directive in some of my projects and I can test the system without running away from the DDD.

Ah, but I want to implement Join anyway. How can I?

You can use Reflection, find out what are the complex objects of your Model and load them into the selection method, which, again I must say, is completely unnecessary. The following construction:

var objeto = contexto.Objetos
                     .Include(o => o.ClasseRelacionada1)
                     .Include(o => o.ClasseRelacionada2)
                     .Include(o => o.ColecaoDeClassesRelacionadas1)
                     .Include(o => o.ColecaoDeClassesRelacionadas2)
                     .SingleOrDefault(o => o.ObjetoId == id);

Already does this job of joins for you.

  • I took a course with MVP from Microsoft and he recommended this type of DDD-based architecture, as I understand it, I agree with you that it gets very complex, but you would have some example of how to do it with example I put, because my team is running late myself, I’m lost.

  • Here may find a more recent article on how to implement unit testing by taking advantage of changes made to that effect in EF6.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.