Generic Dbcontext Dbset Method with Entity Framework

Asked

Viewed 441 times

3

I have an application in C# using Entity Framework.

All of mine DbSet of DbContext, I extend them to have a default search for the grid, below example of the method.

public static GridDTO GridPadrao(this DbSet<Cliente> entities, ParametrosGridDTO parametros, UsuarioLogadoDTO usuarioLogado) 
{
  // Código de busca
}

But to make the call I do as below, I have to put if for each DbSet.

else if (parametros.tipoPesquisa == "Cliente")
    return db.Cliente.GridPadrao(parametros, usuarioLogado);
else if (parametros.tipoPesquisa == "Filial")
    return db.Filial.GridPadrao(parametros, usuarioLogado);

Doubt

Is there any way I can call GridPadrao generic form? No need to put if for each DbSet that exists in the DbContext.

Examples

public static GridDTO GridPadrao(this DbSet<Cliente> entities, ParametrosGridDTO parametros, UsuarioLogadoDTO usuarioLogado)
{
    return new GridDTO(
        entities
            .Where(w => w.Apagado == "N" && w.VisaoID == usuarioLogado.VisaoID)
            .Where(MontaWhere(parametros), parametros.filtro.ToLowerNull())
            .Select(s => new { s.ClienteID, Nome = s.Pessoa.Nome, CNPJCPF = s.Pessoa.CNPJCPF, Fixo = s.Fixo == "S" ? "Sim" : "Não" })
            .OrderBy(MontaOrderBy(parametros))
            .Skip(parametros.itensParaIgnorar)
            .Take(parametros.itensPorPagina)
            .ToArray(),
        entities.TotalDeRegistros(parametros, usuarioLogado)
    );
}

and

public static GridDTO GridPadrao(this DbSet<Funcao> entities, ParametrosGridDTO parametros, UsuarioLogadoDTO usuarioLogado)
{
    var setorID = Convert.ToInt32(parametros.filtrofixo);

    return new GridDTO(
        entities
            .Where(w => w.SetorID == setorID && w.Apagado == "N" && w.VisaoID == usuarioLogado.VisaoID)
            .Where(MontaWhere(parametros), parametros.filtro.ToLowerNull())
            .Select(s => new { s.FuncaoID, s.Sigla, s.Descricao })
            .OrderBy(MontaOrderBy(parametros))
            .Skip(parametros.itensParaIgnorar)
            .Take(parametros.itensPorPagina)
            .ToArray(),
        entities.TotalDeRegistros(parametros, usuarioLogado)
    );
}
  • The default grid method is set once for each entity, right? Can you post at least two of them? Because if they look alike, you can put it all together.

  • @jefersonb added two examples.

  • Yes, it won’t be able to unite everything. I thought about creating a contract between the entities, but interfaces do not allow the creation of static methods. Well, I need to go away for a few minutes, try to think of something when I get back.

1 answer

5


Is there any way I can call GridPadrao generic?

Yes. Using generic.

public static DbSetExtensions
{
    public static GridDTO GridPadrao<T>(this IDbSet<T> entities, ParametrosGridDTO parametros, UsuarioLogadoDTO usuarioLogado)
        where T: class
    {
        // Código
    }
}

The problem is that you used a number of strong typages there. Possibly the method will have to be redesigned integer to work generically.

  • And how do I get Dbset from a string? Besides, you would have to think of a way to have the fields of select, Where and order by, would that be possible? It would be a repository?

  • Why do you want to get the Dbset from a string? I think you’re complicating too much. A way would be to use Reflection + Attributes, but for sure this will get huge.

  • My idea was to stop having the if. But apparently it’s easier to leave it that way so it doesn’t get too complicated.

Browser other questions tagged

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