-- Ludwig Ruderstaller <[email protected]> wrote
(on Friday, 21 September 2012, 12:32 PM +0200):
> Hi,
> 
> After reading much about the Service Manager implementation in ZF2 and
> trying out a few things, I think I finally get the concept and the idea
> behind it.
> 
> But please help me understand one thing.
> 
> Imagine I have a medium size database with 10 entities. Along them are
> 10 form definition.
> 
> The input filter is described in the entity.
> 
> in the service_manager I have a factory for each entity. (as the code is
> mostly the same the factories extend from an base entityFactory).
> then I have a factory for each Form (again extended by a base
> formFactory) which along other things inject the input filter from the
> entity.
> 
> Is this really the right way?
> 
> It seems extremly redundant to do this for each entity/form.

Use an initializer instead. The process is quite simple:

 * Define an interface for injecting the common input filter and/or
   entity, and name it something like "SomeEntityAwareInterface". Define
   a setter method for the input filter or entity.
 * Attach an initializer to the service manager:
    
    'initializers' => array(
        function ($instance, $services) {
            if (!$instance instanceof SomeEntityAwareInterface) {
                return;
            }
            $entity = $services->get('SomeEntity');
            $instance->setSomeEntity($entity);
        },
    ),

(The above should likely go in a Module class, or be defined as a class
implementing Zend\ServiceManager\InitializerInterface -- in which case
you can specify the class name of the initializer in the config
instead.)

This is a very cheap way to automate injections when you know many
objects in your application will need to receive the same object
instances. What it means is that you can define your objects as
invokables, which largely simplifies configuration. (This is why most
controllers can be defined as invokables, because we have several
initializers for common interface injections.)

> There are abstract_factories which can work around this, but after what
> I have read, this is more of a fallback. Is this so? Can't I not just
> have a formFactory and a entityFactory? I would just have to implemnt
> the canCreateServiceWithName method to only target form respectively
> entity classes.
> 
> Is there a downside to have multiple abstract factory classes?

Not really, particularly if you have a nice, static list of classes it
will manage. Remember, an AbstractFactory will define methods both for
retrieving an instance, as well as indicating whether or not it can
handle a particular instance; the former method will not be called if
the latter returns false.

I'd go with initializers if there's only one or two common injections,
and an abstract factory if the instantiation logic is more complex.

-- 
Matthew Weier O'Phinney
Project Lead            | [email protected]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to