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.
– Jéf Bueno
@jefersonb added two examples.
– Tiedt Tech
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.
– Jéf Bueno