Start hereQuick start
Quick start
This page installs the package, combines a source into a provider, and shows the collection rebuilding when the source changes.
Install
Section titled “Install”dotnet add package NCode.LiveCollections<PackageReference Include="NCode.LiveCollections" />NCode.LiveCollections.Abstractions alone carries the interfaces.
Register, create, read
Section titled “Register, create, read”This sample registers the library, wraps an observable collection as a data source, creates a provider over it and reads the collection. It then subscribes to the provider’s change token and adds an item.
services.AddLiveCollections();await using var serviceProvider = services.BuildServiceProvider();
var dataSourceFactory = serviceProvider.GetRequiredService<ICollectionDataSourceFactory>();var providerFactory = serviceProvider.GetRequiredService<ICollectionProviderFactory>();
var observable = new ObservableCollection<string> { "alpha" };using var dataSource = dataSourceFactory.CreateObservable(observable);
await using var provider = providerFactory.Create( collectionFactory: items => items.ToList(), dataSource: dataSource, owns: false);
var current = provider.Collection;
IReadOnlyList<string>? rebuilt = null;using var subscription = ChangeToken.OnChange(provider.GetChangeToken, () =>{ rebuilt = provider.Collection;});
observable.Add("beta");What happened
Section titled “What happened”- The provider builds its collection on the first read.
- Adding
"beta"signals the observable source’s change token. - The provider rebuilds its collection and then signals its own change token.
ChangeToken.OnChangesubscribes again after each signal, so the callback runs for every change.
Next steps
Section titled “Next steps”- Combine data sources into one provider.
- React to changes in the collection.
- Look up a type in the API map.