title: How should I use MVVM and Dependency Injection when I need to fetch a controller data from two different web services?
body: <p>I use MVVM design pattern in my swift project. In a view controller I need to get data from two different web services. So I need to create a view model in the controller like below:</p>
  
  <pre><code>class MyViewController: UIViewController {
  
  var viewModel: MyViewModel = MyViewModel(repository: NetworkLayer(service1: WebService1(), service2: WebService2()))
  
      override func viewDidLoad() {
           super.viewDidLoad()
           viewModel.getData(catUrl, categoryId: "\(catId)")
      }
  } 
  </code></pre>
  
  <p>The viewmodel is responsible to fetch data from servers and update the views via controller. I found Repository design pattern and Dependency Injection to solve my problem :</p>
  
  <pre><code>class MyViewModel {
      var webService1: WebService1?
      var webService2: WebService2?
  
      init(repository: NetworkLayer) {
          self.webService1 = repository.service1
          self.webService2 = repository.service2
      }
  
      func getData(_ url: String, categoryId: String) {
          webService1?.delegate = self
          webService1?.getData(urlCode: url)
  
          webService2?.delegate = self
          webService2?.getProducts(queryString: "sortby=14&amp;pageno=0&amp;status=2&amp;pagesize=20&amp;category=c\(categoryId)")
      }
  }
  
  class NetworkLayer: WebService1Delegate, WebService2Delegate {  
      var webService1: WebService1?
      var webService2: WebService2?
  
      init(service1: WebService1, service2: WebService2) {
          self.service1 = service1
          self.service2 = service2
      }
  }
  </code></pre>
  
  <p>Can someone tell me is my way to handle this situation correct? If not, what is the best way to cope with?</p>