vendredi 18 août 2017

Ideas on refactoring controller JAX-RS Servlet

I have a JEE based JAX-RS controller servlet that implements a search API. At a high level:

  1. It intercepts multiple request params.
  2. Based on each request param, it incrementally adds on filters to a search query
  3. It fires the search query against a back end search engine and gets back the response. The pseudo-code looks something like:

    //Start assembling JSON based query object 'jsonQueryObject' //If (param1) { // jsonQueryObject.put(newJSONObject) //} //If (param2 || param3) { // jsonQueryObject.put(newJSONObject) // if ( param2 ) { // jsonQueryObject.put(newJSONObject) // } // if ( param3 ) { // jsonQueryObject.put(newJSONObject) // } //If (param4) { // jsonQueryObject.put(newJSONObject) //} //... // if (param n){ // jsonQueryObject.put(newJSONObject) //} //response = fireSearch(jsonQueryObject) //return response;

... and so on.
Here are some issues that I'm looking to address which is where I need some thoughts:

  1. Its a single controller class with big single method - that is getting bigger whenever we support another filter via request param
  2. Every time a new filter is added a new request parameter we end up modifying same controller class and in fact same method - lead to forever modified and potentially destabilized code.

I'm looking for thoughts/suggestions/ideas on how to best refactor/break this code up for better maintenance.

Aucun commentaire:

Enregistrer un commentaire