In Magento 2, would it be wrong to create a Factory model that allows me to get a factory for any of the models in my module? The reason I ask is that injecting model factories into my blocks/controllers where I need them and it is becoming quite difficult to manage. The proposed Factory of factories model I propose would still use DI to inject the factory classes so nothing would be hardcoded.
Here's some example code:
namespace Vendor/Module/Model;
class Factory
{
protected $_factories = null;
public function __construct(
FirstModelFactory $firstModelFactory,
SecondModelFactory $secondModelFactory
) {
$this->_factories = array(
'FirstModelFactory' => $firstModelFactory,
'SecondModelFactory' => $secondModelFactory
);
}
public function getFactory($class)
{
return isset($this->_factories[$class])
? $this->_factories[$class]
: false;
}
}