Pick the data source for where your items come from and what you do with them. Answer the questions, and the card for your data source is marked below, with how to create it, what to watch for and the guide that goes further. The answers stay in the page’s address, so you can paste a choice into a pull request.
- What do you need?
- A live collection of items from one place: go to step 2.
- One collection from several sources: go to step 3.
- A new source made from one you already have: go to step 4.
- Where do the items come from, and what changes them?
- What should readers get?
- The collection type they need, such as a
List<T>, rebuilt when any source signals: a provider.
- One source, combined by a function of your own, such as one that removes duplicates: a composite.
- One source, the sources one after another, that you can derive from again: join sources in order.
- What should the new source do with the items?
Every source hands its readers a snapshot, and a provider turns any of them into the collection type your readers need, so most answers end in a provider over the source they name.
- Create it
dataSourceFactory.CreateStatic(items), or new StaticCollectionDataSource<T>(items)- Watch for
- It never signals, so
WaitForChangeAsync on it waits until its cancellation token is cancelled. It disposes its items only with disposeItems: true.
Combine data sourcesIts behavior
- Create it
dataSourceFactory.CreateObservable(observableCollection), or new ObservableCollectionDataSource<T>(observableCollection)- Watch for
- Each edit is one signal and one rebuild, so a burst of edits is a burst of rebuilds: wrap the source in
Debounce. Edit the collection from one thread at a time.
Combine data sourcesIts behavior
- Create it
dataSourceFactory.CreateMutable<T>(), or new MutableCollectionDataSource<T>()- Watch for
- Each call that changes the items is one signal, so
Add in a loop rebuilds once per item: use AddRange, Replace or Update. It never disposes an item it drops.
Push or reload itemsIts behavior
Items from one placeCreateChangeToken
Load items with a function, and load them again each time a change token signals, such as a configuration reload.
- Create it
dataSourceFactory.CreateChangeToken(changeTokenProducer, load), or new ChangeTokenCollectionDataSource<T>(changeTokenProducer, load)- Watch for
- Without a
handleException, the first reload that throws ends reloading. The load runs under the source's lock on the thread that fired the token, so keep it short.
Push or reload itemsIts behavior
Items from one placeCreatePeriodicPolling
Ask a remote service or a database for the items on a timer, and on demand with RefreshAsync, when it offers nothing to subscribe to.
- Create it
dataSourceFactory.CreatePeriodicPolling(state, initialCollection, refreshInterval, refreshCollectionAsync), or new PeriodicPollingCollectionDataSource<TItem, TState>(...)- Watch for
- Without a
handleExceptionAsync, the first failed refresh ends polling. Return Unchanged, or use ChangedIfDifferent, when nothing changed, or every refresh rebuilds.
Poll a remote sourceIts behavior
- Create it
dataSourceFactory.CreateComposite(dataSources), or new CompositeCollectionDataSource<T>(dataSources)- Watch for
- It disposes its sources unless
owns is false, so pass false for sources a container resolved.
Combine data sourcesIts behavior
Combines sourcesConcatSources
Join a source and others, one after another, into one source you can read, feed to a provider or derive from again.
- Create it
source.ConcatSources(others)- Watch for
- It disposes every source it joins unless
owns is false.
Derive a data sourceIts behavior
- Create it
source.SelectItems(item => ...)- Watch for
- LINQ's
Select on Collection copies the items once and never updates. A selector that throws on a rebuild keeps the last items and says nothing.
Derive a data sourceIts behavior
- Create it
source.WhereItems(item => ...)- Watch for
- LINQ's
Where on Collection copies the items once and never updates. A predicate that throws on a rebuild keeps the last items and says nothing.
Derive a data sourceIts behavior
- Create it
source.Transform(items => ...)- Watch for
- The function runs again over every item on each signal, on the thread that signalled, so keep it cheap.
Derive a data sourceIts behavior
- Create it
source.Debounce(quietPeriod, timeProvider)- Watch for
- Readers see the collection from before the burst until the quiet period passes, and a source that never stays quiet that long never signals.
quietPeriod is from 1 to 4,294,967,294 milliseconds.
React to changesIts behavior
Builds your collectionICollectionProviderFactory.Create
Hand readers the collection type they need, such as a List<T> or an ImmutableArray<T>, built from one or more sources and rebuilt when any of them signals.
- Create it
providerFactory.Create(collectionFactory, dataSources), new CollectionProvider<TItem, TCollection>(...), or a registration with AddCollectionProvider- Watch for
- Each signal is one full rebuild, under the provider's lock on the thread that signalled, so keep the collection factory short. It disposes its sources unless
owns is false.
Combine data sourcesIts behavior
Every data source, side by side: behavior of every data source.