Click here to Skip to main content
14,735,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a few functions in my main method for my python application. All these functions perform various tasks by calling different classes under my src folder. Applications using my tool define a configuration file that my tool reads from and perform the tasks respectively. Until now, there is just one path of workflow for all the tasks.
def task1(conf):
 # task1
     
def task2(conf):
 # task2

def main(args):
  args = parse_args(args)
  conf = get_conf(args.config_file)
  args.command(conf=conf)
    
def get_conf(config_file):
  # read the conf and get it as a class obj
    
if __name__ == "__main__":
  sys.exit(main(sys.argv[1:]))

I am now extending the functionality of my toolkit to support a new configuration option which introduces a different path of workflow but with the same set of tasks. So based on the config type, I need to choose which workflow to follow. For this reason, I am trying to refactor my codebase to make it easily extendable and maintainable. I am planning to create an interface for each task and have separate classes for each config type task, and then use dependency injection to create the necessary class for the given config type from within main.

I can use one of the many dependency injection libraries or I can also achieve what I need using a factory class that will be responsible for creating the object based on the config type without having to use a separate DI library.

I am looking for any suggestions on what could be the right approach here. Is there a better alternative that I haven't considered? If anyone has experience using DI libraries in python could you share your experiences?

What I have tried:

I used a factory pattern with inheritance.
Posted
Comments
Gerry Schmitz 3-Nov-20 15:05pm
   
Anything that reduces dependencies on 3rd party libraries is a good thing, IMO.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900