StartQuick 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 changes with OnChange 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 = provider.OnChange(collection => rebuilt = 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.
OnChangecalls the callback with the rebuilt collection after each signal, so it runs for every change.
Register a provider in the container
Section titled “Register a provider in the container”AddCollectionProvider registers a provider in one call. Its factory builds the provider from the container’s services, and AddCollectionProvider adds the library’s services that are missing, as AddLiveCollections() does, so this sample doesn’t call it.
services.AddCollectionProvider(serviceProvider =>{ var dataSourceFactory = serviceProvider.GetRequiredService<ICollectionDataSourceFactory>(); var providerFactory = serviceProvider.GetRequiredService<ICollectionProviderFactory>(); var dataSource = dataSourceFactory.CreateStatic(["alpha", "beta"]);
return providerFactory.Create<string, IReadOnlyList<string>>( collectionFactory: items => items.ToList(), dataSource: dataSource );});await using var serviceProvider = services.BuildServiceProvider();
var provider = serviceProvider.GetRequiredService<ICollectionProvider<string, IReadOnlyList<string>>>();var items = provider.Collection;The container builds the provider once, on the first resolution, and hands every reader that instance. When the container is disposed, with Dispose() or DisposeAsync(), it disposes the provider, and the provider disposes the source it owns. A source the factory resolves from the container goes to the provider with owns: false, because the container disposes it.
Every call adds a registration: GetRequiredService resolves the last one, and GetServices returns them all. AddKeyedCollectionProvider registers a provider under a key, resolved with GetRequiredKeyedService, and its factory also gets the key.
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.