This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17236|Reply: 0

[Communication] Dependency injection, what are the benefits of using it?

[Copy link]
Posted on 2/10/2019 6:14:29 PM | | |
First, the creation of related objects of classes can indeed be done by new. However, when the project is too large and the object has too many dependencies, we need to write a lot of new code when building the object, and there may be a method of directly calling the object after forgetting to instantiate some dependent objects, then there will be a null pointer exception. If this process is left to container management, we don't have to worry about object instantiation, and there will be no null pointer exceptions when using dependent objects at any time, and the code can be simplified.
Second, the attributes used in the method call process may be instance attributes, if they are all defined as static, what about the data used in the method? If you define all methods as static, then the properties of the class referenced in the method must also be defined as static, which is equivalent to global attributes, so there will definitely be data synchronization problems.
In fact, it makes sense to think about the programming principles given in the design pattern, all aimed at building highly readable, easy-to-maintain and scalable code. Best wishes!

It is convenient to assemble software in the way of building blocks, and it is convenient to create software with high cohesion and low coupling (extracting dependencies from the code)
Collaborative development, debugging code (no need to integrate components together, they can be tested separately), and the code created is more robust.

Project development pays attention to high cohesion and low coupling, and the use of dependency injection can avoid using the new keyword area to create objects, thus reducing the coupling between classes






Previous:c# Marshal.PtrToStructure
Next:Encryption and Decryption and Assembly Language
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com