Comments (5)
You can do that if I caught your question correctly.
Here is the document:
https://github.com/Swinject/Swinject/blob/master/Documentation/DIContainer.md#registration-with-arguments-to-di-container
from swinject.
In documentation nothing mentioned about parameters and initCompleted
callback. In my case I have a view model, that depends on some parameters and services. So I registering it like this:
container.register(MenuPageModeller.self) { r, menuId in
MenuPageModel(menuId: menuId, apiService: r.resolve(ApiService.self)!)
}
And I have a view controller, that have public property viewModel: MenuPageModeller
. I'd like to inject this property automatically. Instead of doing this:
let viewModel = container.resolve(MenuPageModeller.self, argument: 123)
let page = MenuPage()
page.viewModel = viewModel
I'd like to do this:
let page = container.resolve(MenuPage.self, argument: 123)
Is that possible? Or I'm trying to do something weird? I'm new to DI :)
from swinject.
You can do that something like this without using initCompleted
if your types do not have circular dependencies.
container.register(MenuPageModeller.self) { r, menuId in MenuPageModel(menuId: menuId) }
container.register(MenuPage.self) { r, menuId in
let page = MenuPage()
page.viewModel = r.resolve(MenuPageModeller.self, argument: menuId)
return page
}
then
let page = container.resolve(MenuPage.self, argument: 123)
(initCompleted
doesn't support taking arguments.)
from swinject.
Thanks! I have already tried this example before asking my question, but compiler didn't understand this code:
container.register(MenuPage.self) { r, menuId in
let page = MenuPage()
page.viewModel = r.resolve(MenuPageModeller.self, argument: menuId)
return page
}
After your answer I tried it again and figured out, that we need to specify closure arguments types:
container.register(MenuPage.self) { (r: ResolverType, menuId: Int) in
let page = MenuPage()
page.viewModel = r.resolve(MenuPageModeller.self, argument: menuId)!
return page
}
And this works as expected.
Thank you again! ๐
from swinject.
@nayzak that's good to hear you got the solutionโ๏ธI forgot mentioning the explicit type specification in case the Swift compiler cannot infer the types.
from swinject.
Related Issues (20)
- UIFoundation: UINibDecoderDecodeObjectForValue exception iOS 16 HOT 2
- "master" branch is ๐ฅ DEAD ๐ฅ HOT 5
- A
- Class with multiple initializers defined with Container scope, creates multiple objects HOT 1
- How to remove (unregister) a dependency with Swinject?
- Provider.instance throws a threading error
- Compiling with xCode 15 beta
- Build issue: Integration of SwiftLint into Xcode project not working on Apple Silicon Macs HOT 1
- Current deployment targets are not compatible with xCode 14.3 HOT 8
- Support for Async Await HOT 9
- Synchronous reads and writes fail
- What is the best way to distribute appDelegate events to all existing components?
- Privacy Manifest HOT 6
- Not building on visionOS HOT 1
- Latest version is not available on CocoaPods HOT 1
- Ownership Transfer of Swinject Project to Faire HOT 2
- Synchronized containers deadlock when reading from multiple threads
- Not able to install the latest version 2.8.4 via cocoapods HOT 3
- Privacy Manifest HOT 1
- Compilation issue with the latest version of 2.8.5 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swinject.