Questions related to a ConcurrentDictionery usage with MVVM pattern?

Hello there,

To put it plainly, I’m a bit confused about the MVVM pattern and I’m new to asychronous programming, hopefully this won’t hurt….

I have a ConcurrentDictionery defined as follows;

ConcurrentDictionary<Guid, PriceTimeSeries> PriceLibrary { get; set; } 

I have a PriceTimeSeries class is as follows;

public partial class PriceTimeSeries { public virtual IEnumerable<CacheMarketRunnerPrice> AvailableToBackAllPrices { get; set; } public virtual IEnumerable<CacheMarketRunnerPrice> AvailableToLayAllPrices { get; set; } public virtual IEnumerable<CacheMarketRunnerPrice> TradedVolume { get; set; } public virtual List<CacheMarketRunnerPrice> AvailableToBackBestPrices { get; set; } public virtual List<CacheMarketRunnerPrice> AvailableToBackMovingAverages { get; set; } public virtual List<CacheMarketRunnerPrice> AvailableToLayBestPrices { get; set; } public virtual DateTime StampRangeMax { get; set; } public virtual DateTime StampRangeMin { get; set; } public virtual DateTime StampSelected { get; set; } public virtual List<DateTime> UniqueStampList { get; set; } etc 

I have a few questions;

a) On which thread should one instantiate the ConcurrentDictionary? Does it even matter? b) Presume I instantiate the ConcurrentDictionery on the UI thread. Then on a background thread run the following code to alter a value in my PriceTimeSeries; If I have my NotifyPropertyChanged set as I would in a normal ViewModel, should my UI successfully reflect changes?

if (!PriceLibrary.ContainsKey(myGuidToFind)) { PriceTimeSeries v = null; while (!PriceLibrary.TryGetValue(runnerGuid, out v)) { } v.StampRangeMin = DateTime.UtcNow; } 

c) Does storing an object in a concurrentDictionary make the entire object threadsafe? For example, is accessing the v.StampRangeMin as per the code above done in a blocking/threadsafe way? i.e. What if another thread changes the StampRangeMin just after I have retrieved the object from the ConcurrentDictionery PriceLibrary? Alternately, would I have to make my PriceTimeSeries class also have blocking collection properties? (and access them accordingly);

 public virtual ConcurrentDictionery<Guid, CacheMarketRunnerPrice> AvailableToBackBestPrices { get; set; } public virtual ConcurrentDictionery<Guid, CacheMarketRunnerPrice> AvailableToBackMovingAverages { get; set; } public virtual ConcurrentDictionery<Guid, CacheMarketRunnerPrice> AvailableToLayBestPrices { get; set; } 

Hopefully this question is not absurd. Thank you kindly for any thoughts. Much appreciate your time.

submitted by /u/BeingFriendlyIsNice
[link] [comments]

Leave a Reply