Skip to content

StartWhich data source?

Which data source?

Answer at most two questions about where your items come from and what you do with them, and get the data source, combinator or provider to use, how to create it and what to watch for.

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.

Answer, and the card for your data source is marked below.

1What do you need?
2Where do the items come from, and what changes them?
3What should readers get?
4What should the new source do with the items?
  1. 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.
  2. Where do the items come from, and what changes them?
  3. 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.
  4. 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.

Items from one place

CreateStatic

Serve a fixed set of items that never changes, such as reference data loaded at startup.

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.
Items from one place

CreateObservable

Follow an ObservableCollection<T> your code edits, such as a list a view binds to.

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.
Items from one place

CreateMutable

Push items from your own code, from any thread, and replace or edit them in one call.

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.
Items from one place

CreateChangeToken

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.
Items from one place

CreatePeriodicPolling

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.
Combines sources

CreateComposite

Combine several sources into one source with a function of your own, such as one that removes duplicates.

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.
Combines sources

ConcatSources

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.
Derives a source

SelectItems

Map each item to another, and map again each time the source changes.

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.
Derives a source

WhereItems

Keep the items that match a predicate, and filter again each time the source changes.

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.
Derives a source

Transform

Reshape the whole collection, such as sort, group or take the first few items.

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.
Derives a source

Debounce

Signal once after a burst of changes goes quiet, so a provider over it rebuilds once per burst.

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.
Builds your collection

ICollectionProviderFactory.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.

Every data source, side by side: behavior of every data source.