0

I am new to DependencyInjection and Swinject and because of that i have question.

Let's say we have a ViewModel PersonList with a list of Person objects. We select one of the Person and pass it to PersonDetails which is something like PersonDetails(person:Person) so basically we are selecting the Person from the first ViewModel and passing it to the second ViewModel where the details of Person will be displayed.

As i have understood with Swinject we are to inject the dependencies at compile-time like

let container = Container()
container.register(Person.self) { _ in Person(name: "Someone") }
container.register(PersonDetails.self) { r in PersonDetails(person: r.resolve(Person.self))}

My question is we do not have the information at compile-time to know what kind of Person will be selected in PersonList so how can we inject the correctly selected Person to PersonDetails at Compile-time.

dev_ios999
  • 339
  • 3
  • 9
  • Why do you need a framework to do dependency injection? – Joakim Danielson Mar 29 '21 at 07:05
  • I mean its just easier right ? Instead of writing my own container. – dev_ios999 Mar 29 '21 at 09:47
  • But do you need this container at all? You say it yourself, pass the person object from the list view model to the detail view model. Why not do that directly in the code rather than going via another class? – Joakim Danielson Mar 29 '21 at 10:18
  • I mean that's how dependency Injection is supposed to work right ? You make a container and only the container is supposed to have all the information available to resolve dependencies so that the classes are not coupled with their dependencies. – dev_ios999 Mar 29 '21 at 11:04

0 Answers0