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.
Four kinds of source
Section titled “Four kinds of source”| Source | Signals when | Collection |
|---|---|---|
| Static | Never, it returns Null |
A copy made at construction |
| Observable | Every Collection event |
The live Observable |
| Composite | Any source or extra producer signals | Combine, 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>.
A provider is a source
Section titled “A provider is a source”BaseCollectionProvider implements ICollectionDataSource<TItem>, so one provider can feed another. AsDataSource() wraps an ICollectionProvider<TItem, IEnumerable<TItem>> as a source.
Who disposes what
Section titled “Who disposes what”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
ownstotrue, and the provider disposes that source inDisposeAsync. - Its several-source constructors default
ownstofalse, because such sources usually come from DI. The provider always disposes its internal composite, and the composite’sOwnsdecides whether it disposes each source. - Extra change-token producers are never disposed.
CompositeCollectionDataSource<T>.Ownsdefaults tofalse.- On static and observable sources,
ownsdefaults totrueand means the items: disposing the source disposes each item when the item typeTis a reference type that implementsIDisposable. An item typed asobject, as an interface withoutIDisposableor 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.