如何在di.dart中創建單身人士實例?單身人士在di.dart
好像如果我這樣做:
module = new Module()
..bind(GameRepository)
..bind(MessageFactory)
..bind(Broadcaster);
我GameRepository
被多次實例化,如果當我做
injector.get(GameRepository)
我試圖做
..bind(GameRepository, toValue: new GameRepository())
這似乎工作,但它很容易,因爲GameRepository
沒有參數,但不應該有辦法告訴di框架將其綁定爲單例,而無需我實例化它(並保持其構造函數調用?)
編輯: @Günter,我確實想要一個Singleton,但是在我的情況下,我想用我的redstone.dart資源中的GameRepository
和外部的。所以,我綁定GameRepository
,然後做到這一點:
// To use it outside of the resource
injector = new ModuleInjector([modules.getProductionModule()]);
// To use it in redstone
app.addModule(module);
現在我可以看到,雷石東持有從一個上GameRepository
不同的參考,我得到當我做injector.get(GameRepository)
我不知道,如果問題變得過於具體:P
我不知道我理解你的解釋。你是否創建了多個'ModuleInjector'實例? –
只有一個,但我認爲redstone.dart創建了自己的'ModuleInjector'。 – Pacane
您是否試圖在紅石註冊您的模塊,並在任何地方使用紅石注入器(https://github.com/redstone-dart/redstone/blob/472ac5144a3dbf9cc14ab32745107d38fdb14fa4/lib/server.dart#L348)? –