MKroeders
MKroeders

Reputation: 7772

Zend framework recurring dependency

In Zend Framework 2 we use the factories to initiate classes. For the factories we make use off the FactoryInterface, which gives you the ability to use the ServiceManager.

Our problems only is with recurring dependency. We have te following

We have the PlaceRepository and PlaceEntity.

The PlaceRepostory is dependend of the PlaceEntity so it knows which entity it needs to return. However the PlaceEntity also needs the PlaceRepostory so it can get a parent place. (like get the country of city x (where both city and country are a place)).

This ends in an forever going dependency.

Now we could inject the service locator in the entity but you loose the ability for easy testing and you can't see of which class it is depended on.

So is there an other option for this or beter option to this problem, so you can still have the dependency but it is not recurring like an inity loop?

Upvotes: 0

Views: 69

Answers (1)

Volkan
Volkan

Reputation: 2210

If dependancy is hierarchical split it into ownership and membership. Try adding object properties which point to members and owner. Since PlaceRepository owns PlaceEntity objects you can implement like that

instancePlaceRepository->members['instancePlaceEntityName'] = instancePlaceEntity;

and

instancePlaceEntity->owner = instancePlaceRepository;

Which will put everything in order.

You can walk down the members and climb up the owners.

If you have Country->City->Street type dependancies which, everything is placeEntity and a single placeRepository holds everything then eliminate ownership of PlaceRepository, this time build the hierarchy around the places. Country->members have cities and towns and cities->members have streets, buildings etc

Upvotes: 1

Related Questions