Context.getXxxService() used instead of "this"

I’m currently writing unit tests for org.openmrs.api.ProgramWorkflowService and a few questions have come up. This is one:

Why is Context.getProgramWorkflowService() used instead of this ? Like in

 public Program getProgram(String name) {
  return Context.getProgramWorkflowService().getProgramByName(name);
 }

Shouldn’t

Context.getProgramWorkflowService()

just return the instance of ProgramWorkflowServiceImpl already in use? (And if it is a different one why would that be necessary?)

Why not

public Program getProgram(String name) {
 return this.getProgramByName(name);
}

?

Context is used to authenticate to the database and obtain services in order to interact with the system…

Context helps you to access the api hence making the service available!!

@farndt the recommended way is NOT to call the method directly within the same service, but rather to call Context.getXXXService.method(), which then calls the method on the proxy, rather than the target itself. That way, the AOP around the method is applied. https://wiki.openmrs.org/display/docs/OpenMRS+AOP

1 Like