How to pass parameters for a query in a repository?

Asked

Viewed 133 times

1

I’m creating a series of classes that return listings in Java (Android).

I am concerned about the format of the parameters that will be passed. My initial approach has been to pass a Map with parameters, thus I can add or remove parameters without major changes in layers.

public List<PlPg> getLista(SQLiteDatabase p_db,Map<String, Object> parametros)

However, what has proved nauseating is that whenever I call this function I need to 'check' the name of the Keys of the parameters that are used within the function.

// Aqui eu pego os parâmetros do map.
int pzmax = (Integer)parametros.get("pzmax");
int cliente = (Integer)parametros.get("cliente")

// Mas aqui, na hora de gerar o Map de parâmetros preciso conhecer as keys .
Map<String, Object> parametros = new HashMap<String, Object>();
parametros.put("pzmax", p_pzmax);
parametros.put("cliente", p_cliente);
parametros.put("plpgpadrao", p_plano);

My alternative idea would be, receive - rather than a Map - an object that has the methods that return the filter values to me.

public List<PlPg> getLista(SQLiteDatabase p_db,IFiltroPlpg pFiltro);

//Essa seria interface q retorna os dados dos filtros
public interface IFiltroPlpg  {

    int pzmax();    
    int cliente();
    int planoPadrao();

}

However, with this approach, I need to create a filter class for each listing configuration, with different getters.

I would like to know which of these approaches would be the most correct from the point of view of OOP best practices, or if there are other better options than this.

  • What is the language, young man? Indeed, your question is only to ask our opinion on the solution?

  • It’s just opinion, I want to understand which is less painful ( could be java) Thanks.

  • Or maybe a third option tbm =)

  • It’s complicated because it’s not part of the site’s scope (see [help/on-topic]).

  • I understand, I imagined that I would get into some concept with best practices in POO ... Anyway, thank you

  • 2

    Anyway, I think your question has salvation. You need to edit it and be more specific, instead of asking for opinions, ask for concrete alternatives on how to do it. Set a goal, explain where you want to go, how far you have gone and what doubts you have. Be clear, succinct and short-sighted. You can [Dit] your question at any time. If you improve it, and I think you’ve really changed something, I withdraw my vote to close.

Show 1 more comment
No answers

Browser other questions tagged

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