jeudi 22 juin 2023

Generic Repository using EF

I'm trying to build generic repository which I will use to get data with or without includes, add data, update data, delete data. I searched a bunch of websites, but still nothing to match my requests.

I have following code which has problem saying that I should be explicit about the type of set and it can't be generic. Do you guys have any ideas on how to build the thing I'm asking for?

public class BaseRepository<T>
{
    private readonly InstagramDbContext _context;

    public BaseRepository(InstagramDbContext context)
    {
        _context = context;
    }

    public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes)
    {
        var query = _context.Set<T>().Where(predicate);
        return includes.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
    }
}

Aucun commentaire:

Enregistrer un commentaire