Skip to content

Instantly share code, notes, and snippets.

@Yeah69
Created October 21, 2017 15:05
Show Gist options
  • Save Yeah69/f3297d2a3aba775c27c919700aea0952 to your computer and use it in GitHub Desktop.
Save Yeah69/f3297d2a3aba775c27c919700aea0952 to your computer and use it in GitHub Desktop.
An example how to use the BFF.DataVirtualizingCollection project
/* IDataVirtualizingCollection<T> implements IList<T>, INotifyPropertyChanged, INotifyCollectionChanged and IDisposable.
Hence, everything you would need to bind to a ItemsSource from a DataGrid, ListBox or other ItemsControls. */
protected IDataVirtualizingCollection<ITitLikeViewModel> CreateDataVirtualizingCollection()
=> CollectionBuilder<ITitLikeViewModel> // A CollectionBuilder creates data virtualizing collections for you.
.CreateBuilder()
/* At the moment, there are three further building functions:
BuildAHoardingSyncCollection(...), BuildAPreloadingAsyncCollection(...), BuildAHoardingAsyncCollection(...).
The async collections need two Scheduler passed to them. A scheduler for the background tasks like the SubscriptionScheduler
below and the DispatcherScheduler like the ObserveScheduler below for UI notifications.
The BasicAccess parameter is crucial for customizing the individual data access in your project.
See the example from my project below. */
.BuildAHoardingPreloadingSyncCollection(
BasicAccess,
PageSize); // The default value is 100. Hence, chunks of size 100 are loaded at a time. However, here you can specify an arbitrary number.
protected IScheduler SubscriptionScheduler = ThreadPoolScheduler.Instance;
protected IScheduler ObserveScheduler = new DispatcherScheduler(Application.Current.Dispatcher);
// For the data acces you'll need to specify how to access specific pages of data and how to get the count
protected IBasicAsyncDataAccess<ITitLikeViewModel> BasicAccess
=> new RelayBasicAsyncDataAccess<ITitLikeViewModel>(
// My project has a Sqlite DB-connection. Hence the Orm.GetPage<T> ultimately uses an SQL-query to get the page
(offset, pageSize) => CreatePacket(Orm.GetPage<ITitBase>(offset, pageSize, _account)),
// Analogously, a query gets the count of the all entries in the database which the query above can get.
() => Orm.GetCount<ITitBase>(_account),
/* Additionaly, a function has to be defined to create placeholders. But only for the async collections.
For sync collection it is sufficient to define a RelayBasicSyncDataAccess<T>, which doesn't require a placeholder function,
instead of a RelayBasicAsyncDataAccess<T>.*/
() => new TitLikeViewModelPlaceholder());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment