mercredi 14 novembre 2018

Alter property of an object used inside the called method in Java

I have a java, spring web application using maven as build mechanism. Consider the following code (over-simplified version of my situation) where a controller calls a service to perform some operation and it turn calls some DAO methods to perform some actions in DB.

class MyController extends Controller {
    public ModelAndView handleRequest(... request) {
        boolean performCheck = Boolean.valueOf(request.getParameter("doCheck"));
        myService.doSomeAction(object)
        return ...; 
    } 
}

class MyService {
    public void doSomeAction(Object o){
        myDao.doSomething(o);
    }
}

class MyDao exterds HibernateDaoSuppot {
    boolean check;

    public void doSomething(Object o){
        if(check == true){
            // some action
        } else {
            // some other action
        }

    }
}

My question is how can I alter the value of the check boolean in the Dao method, based on the value I receive in the controller without explicitly passing the boolean through all the layers? I work with a legacy code with lot of business logics and the business team is not confident of making too many modifications to the existing code. However I am free to add any no of classes or aspects to perform the same.

I have tried reading the call stack in the DAO method and determine the boolean several layers above, but I do not feel good about working with the call stack and I am afraid that some future changes in the app architecture or JVM changes can mess up the call stack.





Aucun commentaire:

Enregistrer un commentaire