Conversation
|
P.S. I am sorry for commit names, I've pushed first commit and than added extra tests and messed with the commit message trying to amend previos commit, but made a new one with the same msg :( |
|
P.P.S. I am also not sure about the name of the feature "sharedConstructor" seems a little misguiding. |
|
There so few cases where you should be calling Is there a reason that named parameters cannot be used here? $dataFinderA = [
'instanceOf' = 'DataFinder',
'constructParams' => [$model, 'a']
];
$dataFinderB = [
'instanceOf' = 'DataFinder',
'constructParams' => [$model, 'b']
];$dice->addRule('DataFinderA', $dataFinderA); $dfA = $dice->create('DataFinderA'); I think if this is really needed, a better and much simpler solution is wildcard names. You'd assign the rule as normal: $dataFinder = [
'shared' => true
]
$dice->addRule('DataFinder, $dataFinder);But when creating the object, provide a unique ID, perhaps something like: $dice->create('DataFinder:' . $id, [$model, $id]);Which, when |
"Share" is a nice option of Dice that guarantees not to create an object, but reuse already created one. But I encounter a lot of situations when I want to get the same object only if constructor parameters are the same. Consider common task like:
Suppose
DataFindercan cache result for searching a record id$idfor a model$model.If we use "shared" rule of Dice, we will always get same instance of
DataFinderdespite that we might want to get an instance ofDataFinderfor the particular$modeland$id.This pull request adds
sharedConstructorrule so you can tell Dice not to create an object if an object of this type was already created with this particular constructor params.In my practice this patterns occurs so many times... I usually do a caching proxy which is okay but it would be nice if we had such a feature in Dice right out of the box.