Which dependency strategy is right for this small application

I have a small command line application I create to learn some common design patterns and oop methods.

I installed all my relevant classes so that they do not create objects inside, but instead they are provided with their own objects, which they require through their constructor.

Now the problem is how to organize everything so that each object gets the required dependencies. I read about containers and frameworks for dependency injection, but this seems redundant for a small command line application + I find it hard to understand how they will fit into my application.

Currently, the stream is as follows:

  • The program is executed by the user on the command line
  • The boot file is loaded, i.e. autoloader, etc.
  • I have a factory method that installs dependencies (all hardcoded inside the class) and returns an application object. There are about 2 dependencies for the main application, and each of them has 2 more dependencies each (this is the difficult part that I think of). Called
  • Application-> run ().

What would be the best approach in terms of the balance between flexibility and simplicity, since I do not believe that the design (around the factory) is quite correct.

+4
source share
2 answers

From the sounds of this application, your application is organized quite well, the design is separate from the application logic, and your dependencies are managed clearly.

Of course, you could add custom dependencies or use the dependency injection infrastructure, however I would recommend avoiding both of them if this does not require an application. If you are starting to use the DI infrastructure, make sure you keep everything connected, even if you use this magic tool and try to drop everything from the frame whenever possible (i.e. have modules that handle internal dependencies through factories, rather than relying on framework). Divide functionality into modules when it makes sense.

I am working on improving a large foggy application that manages all its dependencies through the DI framework, and it starts up slowly and turns into a bit of burlap and doesn’t like to work, it is an application that performs “everything”, and does not delegate tasks to modules and libraries and has zero unit tests.

The main thing is that there are many solutions for each problem, and studying different models is a great idea, there are many types of factory templates, study them all. Some of them are simple, some of them are more complex, you will get an idea of ​​what works in different situations.

My personal recommendation is if your application works the way you intend, it is organized as it seems, and if you haven't done it yet (and the goal of the game is to improve your methods), use:

  • Documentation (write some documentation and drop it to a friend-developer to see how they deal with it)
  • Testing (write some unit tests, and then rewrite the application in another way)
  • Benchmarking (profiling the code and try to find ways to optimize it).

Try a different approach to loading factory, dynamic factory, builder pattern, structure, use benchmarks to understand that you are trading.

0
source

Your application should work as follows:

$app = new Application($arg1, $arg2); $app->run(); 

All other classes must be created in Application and passed as parameters to the constructors of other classes. Rule of thumb: any constructor must have less than 3 arguments . If you follow this rule, everything will be fine.

0
source

All Articles