Required Dependencies During Construction

6 views
Skip to first unread message

livingstn

unread,
Aug 4, 2011, 6:01:42 PM8/4/11
to Symfony Components
I am wondering what the best way to handle the creation of a service
that I would like a dependency passed to at each construction.

For example, I have a Validation class that requires the field name to
be passed in. This class has a dependency on another service, which is
used to charge a test credit card transaction.

So, I imagine my construction would look something like this:

public function __construct($creditCardService, $field)
{
$this->creditCardService = $creditCardService;
parent::__construct($field);
}

What is the best way to construct this object? I don't want to have
the registration specify the field name. So should I only register the
validation class with the Credit Card Service, then it's up to the
user to create the second dependency like so?

$validator = $container->getServiceDefinition('validator')-
>addArgument('credit_card_field');

I suppose the other option is creating a Repository function, which
accepts (and requires) the field name, then passes its own Credit Card
Service dependency. This seems a bit excessive though and it also
doesn't use the container for the actual service:

$validator = $repository->getValidator('credit_card_field');

// Repository Code:
public function getValidator($field)
{
return new Validator($this->creditCardService, $field);
}

What route would you suggest? Any help is appreciated!

Daniel Richter

unread,
Aug 5, 2011, 1:00:45 PM8/5/11
to symfony-c...@googlegroups.com
Hey there,

Since I've used the DI container in sf1.4 projects, I've had to manually inject a few things as well (like the user session for example), so that can be done and works just find. In your case however I would think twice if the field name should really be passed into the constructor. You may just want to instantiate the validator once (passing the CC service) and then use it on different fields by passing the field to the validate() method.. Does that make sense?

Daniel




--
You received this message because you are subscribed to the Google Groups "Symfony Components" group.
To post to this group, send email to symfony-c...@googlegroups.com.
To unsubscribe from this group, send email to symfony-compone...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/symfony-components?hl=en.


livingstn

unread,
Aug 5, 2011, 2:05:29 PM8/5/11
to Symfony Components
Thanks for the response, Daniel.

I definitely agree. Ideally, I would make $field a parameter for the
validate function and simply pass it in that way. Unfortunately, the
framework that I'm using has a requirement for the field to be passed
into the constructor, thus why I'm curious about the preferred
approach.

Do you have any other suggestions for how I could accomplish this?
Thanks again!

Daniel Richter

unread,
Aug 5, 2011, 2:35:27 PM8/5/11
to symfony-c...@googlegroups.com
Is there a method to update the field after instantiation? If so, you can pass a dummy-field into the constructor and set the field later.
If not, I would write a wrapper class for that validator and use that class as a service. That class could take the CC validator as argument, and the field as a method parameter, and on the inside it would instantiate the validator you're having trouble with. That way you're setting yourself up for better times when you're working with a more service-oriented framework as well..

Daniel

livingstn

unread,
Aug 5, 2011, 4:45:05 PM8/5/11
to Symfony Components
Thanks for the help, Daniel!
Reply all
Reply to author
Forward
0 new messages