M
M
Michael2016-08-16 18:38:26
Yii
Michael, 2016-08-16 18:38:26

How to organize a factory?

The application has the following structure:
Component\service\CatalogProductRepository
Component\domain\CatalogProductRepositoryInterface
Component\cache\CatalogProductRepositoryCache
There was a problem using this component. At the moment, for initialization, you need to execute:

$baseModel = new Component\service\CatalogProductRepository;
$cache  = new Component\Cache;
$model = new Component\cache\CatalogProductRepositoryCache($baseModel, $cache);

The idea came to use the factory, but how to do it right is not clear. I see 2 options:
1. Component\factory\CatalogProductRepositoryFactory.
$model = CatalogProductRepositoryFactory::create(true); // create($useCache = false)

But for each entity, you will have to create your own factory.
2. Component\Factory.
$model = Factory::create('CatalogProductRepository', true); // create($model, $useCache = false)

What's better?
ps idea from Sergey Protko

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-08-16
Protko @Fesor

Maybe well him? Really? Try to read this code to someone, it is not readable. I have no idea why you chose to name the product repository CatalogProductRepository. Or why do you need a factory. Well and still - what for you all hands. And what is true.
Both options, as for me, are replete with unnecessary complexity, non-obvious, etc. etc.
I suggest you arm yourself with DependencyInjection. The implementation of this is in any self-respecting framework. And get rid of obscure and implicit constants.
My "idea" was just to use a decorator to implement the caching logic. The essence of the idea is simple - to add caching, you do not need to edit anything either in the logic using the repository or in the repository itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question