New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What to do with singletons? Feature or README? #74
Comments
|
@frederikbosch yup $di->params['PDO'] = array(
'dsn' => getenv('DB_DSN'),
'username' => getenv('DB_USERNAME'),
'passwd' => getenv('DB_PASSWORD'),
);
$di->set('connection', $di->lazyNew('Aura\Sql\ExtendedPdo'));Now you can use Let me know if that helps . Thank you. |
|
@harikt Thank you for your response. But I think that I was not completely clear. I want to register a singleton, so when I instance a new class through auto-resolution, it uses the singleton automatically. So if we combine both our examples. // register the services somewhere
$di->params['PDO'] = array(
'dsn' => getenv('DB_DSN'),
'username' => getenv('DB_USERNAME'),
'passwd' => getenv('DB_PASSWORD'),
);
$di->set('connection', $di->lazyNew('Aura\Sql\ExtendedPdo'));
// is this the valid method for registering a singleton?
$di->types[PDO::class] = $di->lazyGet('connection');
// a class that has a PDO dependency
class SomeClass {
public function __construct(PDO $pdo) {}
}
// somewhere else, using auto-resolution here
$instance = $di->newInstance(SomeClass::class);Now Perhaps PDO is not the best example. Other example: I have a PluginManager which is injected in some controllers. There should be only one PluginManager in my app. When I put the PluginManager dependency in the constructor of the controller, I want to have that one PluginManager automatically (auto-resolution) and not a new instance. How to achieve this? |
|
@frederikbosch Thanks for clarifying. Then your type is nice and there is no other way I am aware of. |
|
Especially for you are trying to make use of auto resolution. |
|
Thank you for the explanation. Why not creating a |
|
for the current time, I don't know :) . |
|
If the feature is agreed on, I am prepared to work on this and issue the pull request. |
|
@frederikbosch I am not sure for a few reasons.
You can always send a PR and wait for Paul to hear / merge it. |
|
👍 |
|
After reading all the comments here, I think this is a documentation fix. "Singletons" are already available as "shared services" via set()/get(), so the main problem appears to be auto-resolving them. I can add something to the README easily enough. |
|
👍 |
What is the best way to register a singleton through the Aura DI container? Let's say PDO. There should be only one instance of PDO through the whole request (in our app), but it might be required to inject it in multiple adapters.
Now I am using types for this purpose.
It is not clear how to do this with the current README, or am I missing something?
Hope you can help. Thanks for the hard work: much appreciated.
The text was updated successfully, but these errors were encountered: