GuidesCombine data sources
Combine data sources
Combine static, observable and other data sources into one provider, choose how they combine, and feed one provider into another.
A provider reads one or more data sources and builds its collection from them. When any source signals, the provider rebuilds. How the rebuild works is on how a provider rebuilds, and who disposes what is on data sources and ownership.
Several sources, one provider
Section titled “Several sources, one provider”Pass the sources as dataSources. The collection factory receives their items concatenated in source order, and it runs again after any source changes.
var dataSourceFactory = serviceProvider.GetRequiredService<ICollectionDataSourceFactory>();var providerFactory = serviceProvider.GetRequiredService<ICollectionProviderFactory>();
var a = dataSourceFactory.CreateStatic(["alpha"]);var b = dataSourceFactory.CreateStatic(["beta"]);
var observable = new ObservableCollection<string> { "gamma" };using var c = dataSourceFactory.CreateObservable(observable);
await using var provider = providerFactory.Create( items => items.ToList(), dataSources: [a, b, c]);
var before = provider.Collection;
observable.Add("delta");
var after = provider.Collection;The static sources never signal. The observable source signals on every CollectionChanged, so adding "delta" rebuilds the collection. A provider made from several sources does not dispose them, so the sample disposes the observable source itself.
Choose how they combine
Section titled “Choose how they combine”CompositeCollectionDataSource<T> combines its sources with CombineFunc. The default concatenates them in source order. Set your own to merge them another way, such as removing duplicates.
await using var combined = new CompositeCollectionDataSource<string>([a, b]){ CombineFunc = sources => sources.SelectMany(s => s).Distinct(), Owns = true,};
var items = combined.Collection.ToList();Owns = true makes the composite dispose its sources when it is disposed. It defaults to false.
A provider as a source
Section titled “A provider as a source”A provider is itself a data source, and AsDataSource() wraps an ICollectionProvider<TItem, IEnumerable<TItem>> as one. A change to the inner source rebuilds the inner provider, which signals the outer one.
var dataSourceFactory = serviceProvider.GetRequiredService<ICollectionDataSourceFactory>();var providerFactory = serviceProvider.GetRequiredService<ICollectionProviderFactory>();
var observable = new ObservableCollection<string> { "alpha" };var source = dataSourceFactory.CreateObservable(observable);
await using var inner = providerFactory.Create<string, IEnumerable<string>>( items => items.ToList(), source);
var innerSource = inner.AsDataSource();
await using var outer = providerFactory.Create( items => items.ToList(), innerSource, owns: false);
var before = outer.Collection;
observable.Add("beta");
var after = outer.Collection;The adapter disposes nothing, so the inner provider stays yours to dispose. The sample passes owns: false to the outer provider for the same reason.
Pitfalls
Section titled “Pitfalls”-
Returning the items as given.
- You write
items => itemsas thecollectionFactory. - What happens: the default
CombineFuncis a deferred query that reads each source when it is enumerated, so the collection can change under a reader, and an observable source’s live collection is not thread-safe. - Write instead: copy the items, as the samples do with
items.ToList().
- You write
-
Passing
owns: truefor sources the container owns.- You write
Create(items => items.ToList(), dataSources: sources, owns: true)with sources resolved from DI. - What happens: the provider disposes them, and the container disposes them again or hands out a disposed source.
- Write instead: leave
ownsat its default offalsefor several sources, and let their owner dispose them.
- You write
-
Expecting the composite to dispose its sources.
- You write
new CompositeCollectionDataSource<string>([a, b])and dispose only the composite. - What happens:
Ownsdefaults tofalse, soaandbstay open. - Write instead: set
Owns = true, or dispose the sources yourself.
- You write