Skip to content

ConceptsData sources and ownership

Data sources and ownership

The four kinds of data source, how a provider is itself a source, and who disposes what.

Every source implements one contract: a Collection and a GetChangeToken(). A provider reads any of them the same way. How a signal becomes a rebuild is on how a provider rebuilds.

Source Signals when Collection
Static Never, it returns NullChangeToken.Singleton A copy made at construction
Observable Every CollectionChanged event The live ObservableCollection<T>
Composite Any source or extra producer signals CombineFunc, concatenation in source order by default, deferred
Periodic polling A refresh returns Changed The last stored collection

The observable source adds no lock around the ObservableCollection<T>, which is not thread-safe, and hands it out live. The composite’s default CombineFunc returns a deferred query that reads each source when it is enumerated. For a snapshot, have the provider’s collection factory copy the items, such as items => items.ToList().

A source of your own implements ICollectionDataSource<T>.

BaseCollectionProvider implements ICollectionDataSource<TItem>, so one provider can feed another. AsDataSource() wraps an ICollectionProvider<TItem, IEnumerable<TItem>> as a source.

A data source can hold a timer, an event subscription or disposable items, and it reaches a provider either made for that provider or resolved from a DI container, which disposes what it resolved. The owns flag settles who disposes it.

  • A provider’s single-source constructors default owns to true, and the provider disposes that source in DisposeAsync.
  • Its several-source constructors default owns to false, because such sources usually come from DI. The provider always disposes its internal composite, and the composite’s Owns decides whether it disposes each source.
  • Extra change-token producers are never disposed.
  • CompositeCollectionDataSource<T>.Owns defaults to false.
  • On static and observable sources, owns defaults to true and means the items: disposing the source disposes each item when the item type T is a reference type that implements IDisposable. An item typed as object, as an interface without IDisposable or as a struct is left alone.
  • A polling source owns neither its state nor its items.
  • The AsDataSource() adapter disposes nothing, so the provider it wraps stays its caller’s to dispose.

The factory defaults match. Create with one source defaults to true, and with several to false. CreateStatic and CreateObservable default to true, and CreateComposite to false.

Disposal is idempotent. After a source is disposed, Collection throws ObjectDisposedException, except on the static source, which keeps serving its copy.