Entity Framework DDD Generic Repository

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 do I do join while I'm in a specific class?

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

How do I use BaseRepository o FaturamentoRepository ?

How to put the inside of faturamentoReposity .

    
asked by anonymous 15.11.2014 / 01:41

1 answer

3

The Entity Framework already implements the Repository Pattern

I've answered this a few times, As in this answer on Unit of Work and Repository but I will write another answer because for some reason the answer is hard to locate.

I'll start with a question withdrawn from Programmers . It is not the accepted answer, but it is the most voted, and is what I consider correct. Here it is said that DbContext already implements a Unit of Work ( 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 statements on hand for your repository .

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

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

This all Entity Framework already does, which makes completely unnecessary to implement yet another layer just to handle 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 the 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 projects of mine and I can test the system without evading the DDD.

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

You can use Reflection, find out what are the complex objects of your Model and load them into the selection method, which, again, I should 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);

Do this joins work for you.

    
15.11.2014 / 04:13