title: Managing IObservable dependencies
body: <p>I have an parameterized Observable that I create using a factory, i.e.</p>
  
  <pre><code>// tick when the day changes in a given timezone
  IObservable&lt;DateTime&gt; GetInstrumentTzDateRollObservable(string timeZoneName) 
  </code></pre>
  
  <p>Under the hood it uses Observable.Timer so there is a cost involved that means I'd not want to create too many of these (the example is simplified, the real life one has much higher cost per observable).</p>
  
  <p>Now, if there is only one consumer everything is simple - we just continue building the pipeline. But the subscribers are dynamic, i.e. the user can open a new screen that will need the same data. It seems what I need is a way to cache observables per timezone so that a consumer would get a stream from this cache if present (GetOrAdd). The simple solution is of course a class that holds a dictionary of these observables. </p>
  
  <p>My question is isn't there a better way to share these intermediate streams by registering them with special DI container or similar which would in essence provide the same functionality as the cache described above. I read enough about service locator anti-pattern and not using DI for anything but instantiation so it seems it is not recommended. Nevertheless I don't really see what my own class would do much differently from a DI container.</p>
  
  <p><strong>Update</strong>: as requested, I want to provide a more realistic scenario of a perceived high cost of generating elements of an observable. I hope this one is clearer.</p>
  
  <p>There is a database that holds data for all instruments. There is a loader that can retrieve data per instrument. There is a notification stream that ticks when the data was updated. The loader also needs the date to prepare the loaded data. The result is then presented as an observable stream:</p>
  
  <pre><code>    IObservable&lt;IntrumentData&gt; GetExpensiveDbDataStreamPerIntrument(string instrumentName)
      {
          // date provider ticks when a day in a given timezone changes prompting us to reload from source
          _dateProvider.GetInstrumentTzDateRollObservable(instrumentName)
              // combine with new data notifications to force a reload
              .CombineLatest(_newDataNotificationObservable.GetNotificationsForInstrument(instrumentName), (date, event) =&gt; date)
              .Select(date =&gt; _dataLoader.LoadData(instrumentName, date))
              // this has been expensive to load so we want to be able to share the result
              .Replay(1).RefCount();
      }
  </code></pre>
  
  <p>So - how can I share these expensive streams when the client can subscribe and unsubscribe dynamically selecting instruments from a long list and using multiple screens that would need the same instrument data?</p>