Sergio
Sergio

Reputation: 2108

Inject the same service instance in many ViewModels

I have a ViewManager class that manages Views instances. It is used by the ShellViewModel and by many other ViewModels, and it has to injected through the constructor. The ShellViewModel has the ViewManager injected. If I create a new ViewModel using a factory, who should provide the factory with the ViewManager in order to inject it into the new VM?

  1. the container? (this means the container injects the ViewManager into the factory, and the factory injects it into the new VM)
  2. the ShellViewModel? (this means the ShellVM does something like factory.Create(this.ViewManager)

what's the best way? Thank you all!

Upvotes: 0

Views: 128

Answers (1)

devdigital
devdigital

Reputation: 34359

The best way if you're using MVVM is to use an MVVM framework. To answer your question, you would want the view manager to be injected via the factory constructor, rather than the factory method, so option 1.

Really though you don't want to be managing view instances yourself - rather use a framework which has already done all of the work for you, rather than reinventing an inferior wheel.

Upvotes: 1

Related Questions