Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,363,119
Community Members
 
Community Events
168
Community Groups

Multiple Classes ... Should I inject, or make Global?

Hello,

I am starting to create "packages" so that my scripting is more stable and more scalable. I am using Jira Server / Scriptrunner as my means of executing said scripts.

For instance, I created a "logger" package to control default behavior on what gets logged and what does not.  "debug" and "info" are normally toggled off, but "error" is normally toggled on

e.g.:

import utils.LogThis

log = new LogThis()
log.enableDebug()
log.debug("This will log because I just turned debug on")
log.info("This will NOT log because info is toggled off by default")

Backstory: I have a "controller" script that imports two other classes.  Logger and Assignee.  Logger is what I just explained, and the Assignee Class handles logic to "re-assign" an issue to another person.  Simple enough.

Issue: I want to set the "log level" for my class in the initial script and have both Logger and Assignee instruction to respect that state.  This is not going to happen if I am "newing" up LogThis in each of the two classes.

Normally, to solve this, I would "inject" the Logger into each class when I "new it up". 

assignee = new Assignee(log, $issue, $assignee)

Is that best way to handle this in Groovy?  I like injection and would normally make a "system" object where I store any / all frameworky objects and just inject that as SOP.

system = new System()

system.log = new LogThis()

system.log.enableDebug()

assignee = newAssignee(system, $issue, $assignee)

As each "script" is compiled to an object, I am thinking I can not really "global" the LogThis as it is probably not available to another "imported" script just by not typecasting / def'ing the variable.

Just looking for "Best Practices" on something like this.   

Thanks in advance

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events