unit testing - Mix services and leaf objects in constructor for dependency injection? -
i attempting make code testable possible, means using dependency injection correctly.
i have read it's okay use new() instantiate object, if object meets criteria. notably - should not accept "non newable" in constructor.
for example, should able go
new form('signup');
because there no way di container know how create "signup" form ahead of time.
i can make work of time, i'd form able validate itself, using third party validator, like:
$form->validate()->isvalid();
...which means have pass in validator service.
i'd prefer have validator included because of time form need validated, , i'd have go through work set validator on own otherwise.
is okay, in instance do:
new form(validator $validator,$name);
i'd value or object object requires in order in valid state 1 of object's dependencies; in example entirely validly include form's name. don't think type of dependency can used whether should injected or not - martin fowler in article instance shows objects having strings injected them, , di containers can configured supply string values.
with in mind, injecting validator , string fine , entirely correct in opinion.
Comments
Post a Comment