A .NET library for building dynamic, change-aware collection providers that aggregate multiple data sources with automatic change notifications.
Issues and discussion are welcome, and code is by invitation while the design settles, as CONTRIBUTING.md says.
A collection provider gives consumers one collection built from several data sources, and an IChangeToken that signals when any of them changes, so the collection is rebuilt only when its data does. Data sources can be static, wrap an ObservableCollection<T>, combine other sources, or poll an external system such as an API, a database or a file.
dotnet add package NCode.Collections.ProvidersOr for abstractions only:
dotnet add package NCode.Collections.Providers.AbstractionsRegister the factories, create a data source and aggregate it in a provider:
services.AddCollectionProviders();
var dataSourceFactory = serviceProvider.GetRequiredService<ICollectionDataSourceFactory>();
var dataSource = dataSourceFactory.CreateStatic(new[] { "item1", "item2", "item3" });
var providerFactory = serviceProvider.GetRequiredService<ICollectionProviderFactory>();
var provider = providerFactory.Create(
collectionFactory: items => items.ToList(),
dataSource: dataSource
);
var items = provider.Collection;ICollectionDataSource<T>- Interface for data sources that provide a collection of items with change token supportICollectionProvider<TItem, TCollection>- Interface for providers that aggregate multiple data sources into a unified collectionISupportChangeToken- Interface for objects that support change notification viaIChangeTokenINullChangeToken- Marker interface for change tokens that never signal changes
- Static Data Source - Wraps an immutable collection that never changes
- Observable Data Source - Wraps an
ObservableCollection<T>with automatic change notifications when items are added, removed, or modified - Composite Data Source - Aggregates multiple data sources into a single unified collection with consolidated change notifications
- Periodic Polling Data Source - Periodically fetches data from external sources (APIs, databases, files) with configurable refresh intervals and error handling
ICollectionDataSourceFactory- Creates data source instances for different scenariosICollectionProviderFactory- Creates collection provider instances that aggregate data sources
- Automatic propagation of change notifications from data sources to consumers
- Lazy initialization of change tokens for efficiency
- Thread-safe collection updates when changes are detected
- Support for additional change token producers beyond data sources (e.g., configuration changes)
AddCollectionProviders()extension method forIServiceCollection- Singleton registration of factories for optimal performance
- Support for DI-managed data source lifetimes
services.AddCollectionProviders();var factory = serviceProvider.GetRequiredService<ICollectionDataSourceFactory>();
var dataSource = factory.CreateStatic(new[] { "item1", "item2", "item3" });var observableCollection = new ObservableCollection<string>();
var dataSource = factory.CreateObservable(observableCollection);
// Changes to observableCollection will trigger change notifications
observableCollection.Add("new item");var dataSource = factory.CreatePeriodicPolling(
state: httpClient,
initialCollection: Array.Empty<User>(),
refreshInterval: TimeSpan.FromMinutes(5),
refreshCollectionAsync: async (client, current, ct) =>
{
var users = await client.GetFromJsonAsync<List<User>>("/api/users", ct);
return RefreshCollectionResultFactory.Changed(users);
},
handleExceptionAsync: async (ex, ct) =>
{
logger.LogError(ex.SourceException, "Failed to refresh users");
}
);var providerFactory = serviceProvider.GetRequiredService<ICollectionProviderFactory>();
var provider = providerFactory.Create(
collectionFactory: items => items.ToList(),
dataSources: serviceProvider.GetServices<ICollectionDataSource<User>>()
);
// Access the aggregated collection
var users = provider.Collection;
// Subscribe to changes
ChangeToken.OnChange(provider.GetChangeToken, () =>
{
Console.WriteLine("Collection changed!");
var updatedUsers = provider.Collection;
});The packages target the .NET LTS releases listed in each project file's TargetFrameworks, as the target framework badge above shows.
- v1.0.0 - Initial release
- Issues: report a bug or ask for a feature.
- CONTRIBUTING.md: how to set up, build, test and propose a change.
- SECURITY.md: how to report a vulnerability privately.
- CODE_OF_CONDUCT.md, GOVERNANCE.md and ROADMAP.md: how we work together, who decides, and where the project is going.
- docs/architecture.md and docs/assurance-case.md: how the project is built, and why it meets its security requirements.
- docs/decisions/: the decisions behind the design, the project's and the template's.
- docs/replies.md: the replies the maintainers post to people outside the project.
- CHANGELOG.md: what changed in each release, in the Keep a Changelog format.
- LICENSE.txt: the license.
Licensed under the Apache License, Version 2.0. See LICENSE.txt for details.
