Resolve class with runtime argument
bozidarlabas opened this issue · 2 comments
Hi,
i want to register some class with init. This class needs one runtime argument and one dependency. I want to resolve this registered class with a runtime argument. Is this possible that DIP takes care of other dependency (which is not runtime argument)?
For example, i have class A which depends on runtime argument SomeViewModel and UseCase which is not a runtime argument. I want to register this class A like this -> container.register ( factory: A.init ). Is it possible to resolve this class by passing a runtime argument? Problem is that this class besides runtime argument has other dependency UseCase which is not runtime argument. I can register class A like this -> container.register { (viewModel: SomeViewModel) in A(viewModel: viewModel, useCase: try self.container.resolve()) }. This works but i want to find a way to register class A like this -> container.register ( factory: A.init ).
EXAMPLE
class SomeViewModel { }
class UseCase { }
class A {
init(viewModel: SomeViewModel, useCase: UseCase) { }
}
class B {
init(a: A) { }
}
func registerDependencies() {
container.register ( factory: A.init ) // -> i want to register class A like this
container.register ( factory: UseCase.init )
container.register { (viewModel: SomeViewModel) in B(a: try! self.container.resolve(arguments: viewModel)) } // -> i want to resolve this class by passing runtime argument but it's not working because DIP try to resolve A with one argument (SomeViewModel)
let test = try! container.resolve(arguments: SomeViewModel()) as B
}
You can resolve UseCase when you resolve A and pass it along with viewModel argument.
Ok. Thanks :) it's working