Dependency Injection: Separating the Call Graph from the Construction Graph -
I'm trying to exercise, and I'm having some difficulty doing this.
I is a function that periodically visits my database, retrieves a list of products, and then sets the battery of tests against those products to determine their security.
If one or more products are found to be unsafe, I need to memorize an ProductRecall object immediately and remotely.
The function looks something like this: (pseudo-code)
void SafteyInspector :: CheckProductSaftey () {database.getProducts (list_of_products) foreach (list_up_products p) {if ( RunBatteryOfTests (p)) unsafe_products.insert (p); } If (unsafe products!). ()) {ProductRecall * Recon = New ProductRecall (unsafe_products); // Oh no! Recall-> Despatch (); }} The problem is that I am "new" - a ProductRecall object in the middle of my call-graph is correct, this dependency violates the principles of injection is. In writing, I can not test without compiling CheckProductSaftey () also ProductRecall .
However, I have my security inspector in the product type << code> object, because safety inspector is the list of unsafe products that determine Does.
I'm using manufacturer-injection for everything, and I would like to continue doing this. Also note that I can issue more than one product record at any given time, so I only assign a single ProductRecall object to SecurityInspector Can not make it in construction.
Any suggestions? Thanks!
I think the problem may be with your implementation of ProductRecall Specifically, if you can call the dispatch () on a newly created object, it shows that the actual functionality is hidden in either the ProductRecall class , Or to give stable members and / or singles to the permanent members to give it and everything else in the product cluster class.
I would recommend creating a class called ProductRecallDispatcher which handles the complexities of one, the real dispatch, then you can make one of these objects, and SafteyInspector Can pass the constructor to. This will make your CheckProductSafety function as follows:
void SafteyInspector :: CheckProductSaftey () {database.getProducts (list_of_products) foreach (list_of_products p) {if ( ! RunBatteryOfTests (p)) Unsafe_products.instate (p); } If (unsafe products!). ()}}
Comments
Post a Comment