If you take a look at the library I've been working on for dependency injection in C++, I recently added an example that mimics something I created for a real project: the ability to inject application configuration directly.
This all works fine as the constructor just asks for a ConfigItem<ConfigTag> type and that is magic开发者_开发问答ally delivered.
A problem occurs when I need to access all of them at the same time (say for a configuration dialog).
The stupid solution I came up with was to generate, using the preprocessor, a class that gets all the configuration items in the constructor as shown here.
This problem would be "nicely" solved if I could inject the injector as I would then only have to generate the code to collect all the ConfigItem instances as shown here.
Note that all the ConfigItems are within a singleton scope.
Hope this question makes sense: how would you do this?
Sounds like you have re-invented the Service Locator Pattern
精彩评论