Skip to content

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.

Terminal window
dotnet add package NCode.LiveCollections

NCode.LiveCollections.Abstractions alone carries the interfaces.

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");
  • 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.OnChange subscribes again after each signal, so the callback runs for every change.