How to implement a Person repository function for the Generic - Asp.net MVC and EF Core repository

Asked

Viewed 192 times

1

I have a function to return the highest value of a table ID field using EF Core. Only that I would like to implement it in my generic repository to be dynamic and to be used by all classes. How do I do this?

//Funcionar no Repositório de Pessoa
public int GetMax()
{
  return DbSet.Select(p=> p.Id).DefaultIfEmpty(0).Max();
}

//Repositório Genérico
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
    protected readonly RetaguardaContext Db;
    protected readonly DbSet<TEntity> DbSet;

    public Repository(RetaguardaContext context)
    {
        Db = context;
        DbSet = Db.Set<TEntity>();
    }

    public virtual void Add(TEntity obj)
    {
        DbSet.Add(obj);
    }

    public virtual TEntity GetById(int id)
    {
        return DbSet.Find(id);
    }

    public virtual IQueryable<TEntity> GetAll()
    {
        return DbSet;
    }

    public virtual void Update(TEntity obj)
    {
        DbSet.Update(obj);
    }

    public virtual void Remove(int id)
    {
        DbSet.Remove(DbSet.Find(id));
    }

    public int SaveChanges()
    {
        return Db.SaveChanges();
    }

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

    public void Remove(params object[] keyValues)
    {
        DbSet.Remove(DbSet.Find(keyValues));
    }

    public TEntity GetByIds(params object[] keyValues)
    {
        return DbSet.Find(keyValues);
    }       
}

1 answer

1


You will need to create a Expression<Func<TEntity, int>> to work your expression by choosing the desired field, example:

In his class of Interface create a method:

int GetMax(Expression<Func<TEntity, int>> select);

then implements in its concrete class:

public int GetMax(Expression<Func<TEntity, int>> select)
{
    return DbSet.Select(select).Max();
}

this example of right with the maximum value contained in some field of its table, but, the expression for Select has 4 overloads (overloads) that can be implemented, but the one in the answer is related to your question.

In use you need to identify the field that needs to recover the maximum value:

Model example:

public class Cliente 
{
    public int Codigo { get; set; }
    public string Nome { get; set; }
}

Code:

Repository<Cliente> rep = new Repository<Cliente>(new RetaguardaContext());
var maximo = rep.GetMax(x => x.Codigo)

The above code is a good code because it also prevents the typing of fields that are not of the type int, but, if you want to leave the code more generic can be created another method in the Interface

T GetMax<T>(Expression<Func<TEntity, T>> select);

and implement as follows:

public T GetMax<T>(Expression<Func<TEntity, T>> select)
{
    return DbSet.Select(select).Max();
}

and use in the same way as the other:

var maximo = c.GetMax(x => x.Codigo);

where the functionality increases for other types of data (int, long, etc.);

References:

  • 1

    Thanks for the Excellent Reply @Virgilio Novic!!!! It worked 100%

Browser other questions tagged

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