Dynamic Data
Dynamic Data is a portable class library that brings the power of reactive (rx) to collections.
A collection that mutates can have ads, updates, and removes. Out-of-the-box, rx does nothing to manage any changes in a collection. That is why Dynamic Data exists. In Dynamic Data, collection changes are notified via an observable changeset that is the heart of the system. An operator receives these notifications and then applies some logic and subsequently provides its own notifications. In this way, operators can be chained together to apply powerful and often very complicated operations with some very simple fluent code.
The benefit of at least 40 operators, which are borne from pragmatic experience, is that the management of in-memory data becomes easy and it is no exaggeration to say it can save thousands of lines of code by abstracting complicated and often repetitive operations.
Getting started
As stated in the blurb, Dynamic Data is based on the concept of an observable changeset. The easiest way to create one is directly from an observable.
IObservable<T> myObservable;
IObservable<IEnumerable<T>> myObservable;
// Option 1: Create an observable where items are identified using the hash code.
var mydynamicdatasource = myObservable.ToObservableChangeSet();
// Option 2: Specify a key like this.
var mydynamicdatasource = myObservable.ToObservableChangeSet(t => key);
The problem with the preceding is the collection will grow forever so there are overloads to specify size limitations or expiry times (not shown).
To have much more control over the root collection then we need a local data store that has the requisite crud methods. Like the preceding, the cache can be created with or without specifying a key as in the following.
// 1. Create a cache where items are identified using the hash code.
var mycache = new SourceCache<TObject>();
// 2. Or specify a key like this
var mycache = new SourceCache<TObject, TKey>(t => key);
One final out-of-the-box way to create an observable change set is if you are doing UI work and have an observable collection, you can do this.
var myObservableCollection = new ObservableCollection<T>();
// Option 1: Create a dynamic data source using hash code for item identification
var myDynamicDataSource = myObservableCollection.ToObservableChangeSet();
// Option 2: Specify a key selector to identify items in the dynamic data source
var myDynamicDataSource = myObservableCollection.ToObservableChangeSet(t => key);
One other point worth making here is any steam can be covered as a cache.
var myCache = someDynamicDataSource.AsObservableCache();
This cache has the same connection methods as a source cache but is read-only.
Jason GreatPosted May 23, 2019, 11:47 AM
If I do var person = _myList.Connect().WhenAnyPropertyChanged();, it should return a IObservable<IChangset<OrderInfo>. Person should contains a collection of what have changed. But I don't quite know how to consume it. Please help
Jason GreatPosted May 23, 2019, 11:44 AM
I think I am missing something, my understanding is if I do
Vithal WadjePosted Feb 3, 2015, 9:48 AM
nice,welcome to C# corner,keep it up