< Summary

Information
Class: MyNet.Observable.Collections.ExtendedCollectionBuilder<T>
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/ExtendedCollectionBuilder.cs
Tag: 323_28699572109
Line coverage
51%
Covered lines: 29
Uncovered lines: 27
Coverable lines: 56
Total lines: 257
Line coverage: 51.7%
Branch coverage
60%
Covered branches: 6
Total branches: 10
Branch coverage: 60%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%22100%
From(...)100%11100%
FromObservable(...)100%210%
FromSnapshot(...)100%210%
FromProvider(...)100%210%
Where(...)100%11100%
And(...)100%210%
Or(...)100%210%
And(...)100%210%
Or(...)100%210%
OrderBy(...)100%11100%
OrderByDescending(...)100%210%
ThenBy(...)100%210%
ThenByDescending(...)100%210%
GroupBy(...)100%210%
ThenGroupBy(...)100%210%
Build(...)50%4490.9%
BuildSelectable(...)50%4481.81%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/ExtendedCollectionBuilder.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ExtendedCollectionBuilder.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.ComponentModel;
 10using System.Linq.Expressions;
 11using System.Reactive.Concurrency;
 12using DynamicData;
 13using MyNet.Observable.Collections.Filters;
 14using MyNet.Observable.Collections.Grouping;
 15using MyNet.Observable.Collections.Selection;
 16using MyNet.Observable.Collections.Sorting;
 17using MyNet.Observable.Collections.Sources;
 18using MyNet.Primitives.Providers;
 19
 20namespace MyNet.Observable.Collections;
 21
 22/// <summary>
 23/// Provides a fluent API for constructing an <see cref="ExtendedCollection{T}"/> instance with specified source, filter
 24/// </summary>
 25/// <typeparam name="T">The type of elements in the collection.</typeparam>
 26public sealed class ExtendedCollectionBuilder<T>
 27    where T : notnull
 28{
 929    private readonly FilterBuilder<T> _filterBuilder = FilterBuilder<T>.Create();
 930    private readonly SortingBuilder<T> _sortingBuilder = SortingBuilder<T>.Create();
 931    private readonly GroupingBuilder<T> _groupingBuilder = GroupingBuilder<T>.Create();
 932    private Func<SourceEngine<T>> _sourceFactory = SourceEngine<T>.Empty;
 33
 34    /// <summary>
 35    /// Defines the source of items for the collection. The provided items will be loaded into the collection at constru
 36    /// </summary>
 37    /// <param name="items">The initial set of items for the collection.</param>
 38    /// <param name="isReadOnly">Determines whether the collection operates in read-only mode.</param>
 39    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 40    public ExtendedCollectionBuilder<T> From(IEnumerable<T> items, bool isReadOnly = false)
 41    {
 942        _sourceFactory = () => SourceEngine<T>.From(items, isReadOnly);
 943        return this;
 44    }
 45
 46    /// <summary>
 47    /// Defines the source of items for the collection using a custom provider. The provided items will be loaded into t
 48    /// </summary>
 49    /// <param name="source">The observable source of change sets for the collection.</param>
 50    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 51    public ExtendedCollectionBuilder<T> FromObservable(IObservable<IChangeSet<T>> source)
 52    {
 053        _sourceFactory = () => SourceEngine<T>.FromObservable(source);
 054        return this;
 55    }
 56
 57    /// <summary>
 58    /// Defines the source of items for the collection using a snapshot provider. The provided factory function will be 
 59    /// </summary>
 60    /// <param name="factory">A function that provides a snapshot of the collection's state.</param>
 61    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 62    public ExtendedCollectionBuilder<T> FromSnapshot(Func<IEnumerable<T>> factory)
 63    {
 064        _sourceFactory = () => SourceEngine<T>.FromSnapshot(factory);
 065        return this;
 66    }
 67
 68    /// <summary>
 69    /// Defines the source of items for the collection using a custom provider. The provided provider will be used to re
 70    /// </summary>
 71    /// <param name="provider">The custom provider that will be used to retrieve the initial set of items for the collec
 72    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 73    public ExtendedCollectionBuilder<T> FromProvider(IItemsProvider<T> provider)
 74    {
 075        _sourceFactory = () => SourceEngine<T>.FromProvider(provider);
 076        return this;
 77    }
 78
 79    /// <summary>
 80    /// Defines the source of items for the collection using a custom provider. The provided factory function will be in
 81    /// </summary>
 82    /// <param name="predicate">A lambda expression that defines the filter criteria.</param>
 83    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 84    public ExtendedCollectionBuilder<T> Where(Expression<Func<T, bool>> predicate)
 85    {
 386        _filterBuilder.Where(predicate);
 387        return this;
 88    }
 89
 90    /// <summary>
 91    /// Defines a logical AND grouping of filter criteria for the collection. The provided predicate will be combined wi
 92    /// </summary>
 93    /// <param name="predicate">A lambda expression that defines the filter criteria for the AND operation.</param>
 94    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 95    public ExtendedCollectionBuilder<T> And(Expression<Func<T, bool>> predicate)
 96    {
 097        _filterBuilder.And(predicate);
 098        return this;
 99    }
 100
 101    /// <summary>
 102    /// Defines a logical OR grouping of filter criteria for the collection. The provided predicate will be combined wit
 103    /// </summary>
 104    /// <param name="predicate">A lambda expression that defines the filter criteria for the OR operation.</param>
 105    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 106    public ExtendedCollectionBuilder<T> Or(Expression<Func<T, bool>> predicate)
 107    {
 0108        _filterBuilder.Or(predicate);
 0109        return this;
 110    }
 111
 112    /// <summary>
 113    /// Defines a logical AND grouping of filter criteria for the collection. The provided group function allows for the
 114    /// </summary>
 115    /// <param name="group">A function that defines the nested set of filter criteria.</param>
 116    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 117    public ExtendedCollectionBuilder<T> And(Func<FilterBuilder<T>, FilterBuilder<T>> group)
 118    {
 0119        _filterBuilder.And(group);
 0120        return this;
 121    }
 122
 123    /// <summary>
 124    /// Defines a logical OR grouping of filter criteria for the collection. The provided group function allows for the 
 125    /// </summary>
 126    /// <param name="group">A function that defines the nested set of filter criteria.</param>
 127    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 128    public ExtendedCollectionBuilder<T> Or(Func<FilterBuilder<T>, FilterBuilder<T>> group)
 129    {
 0130        _filterBuilder.Or(group);
 0131        return this;
 132    }
 133
 134    /// <summary>
 135    /// Defines the primary sorting key for the collection. The provided key expression will be used to sort the items i
 136    /// </summary>
 137    /// <param name="key">A lambda expression that defines the key for sorting.</param>
 138    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 139    public ExtendedCollectionBuilder<T> OrderBy(Expression<Func<T, object?>> key)
 140    {
 3141        _sortingBuilder.Add(key, ListSortDirection.Ascending);
 3142        return this;
 143    }
 144
 145    /// <summary>
 146    /// Defines the primary sorting criterion for the collection, specifying that the items should be sorted in descendi
 147    /// </summary>
 148    /// <param name="key">A lambda expression that defines the key for sorting.</param>
 149    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 150    public ExtendedCollectionBuilder<T> OrderByDescending(Expression<Func<T, object?>> key)
 151    {
 0152        _sortingBuilder.Add(key, ListSortDirection.Descending);
 0153        return this;
 154    }
 155
 156    /// <summary>
 157    /// Adds a secondary sorting key to the collection. The provided key will be used for sorting items that are conside
 158    /// </summary>
 159    /// <param name="key">A lambda expression that defines the key for sorting.</param>
 160    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 161    public ExtendedCollectionBuilder<T> ThenBy(Expression<Func<T, object?>> key)
 162    {
 0163        _sortingBuilder.ThenBy(key);
 0164        return this;
 165    }
 166
 167    /// <summary>
 168    /// Adds a secondary sorting criterion to the collection, allowing for multi-level sorting. The provided key express
 169    /// </summary>
 170    /// <param name="key">A lambda expression that defines the key for sorting.</param>
 171    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 172    public ExtendedCollectionBuilder<T> ThenByDescending(Expression<Func<T, object?>> key)
 173    {
 0174        _sortingBuilder.ThenByDescending(key);
 0175        return this;
 176    }
 177
 178    /// <summary>
 179    /// Adds a grouping key to the collection. Items will be grouped by the provided expression.
 180    /// </summary>
 181    /// <param name="key">A lambda expression that defines the grouping key.</param>
 182    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 183    public ExtendedCollectionBuilder<T> GroupBy(Expression<Func<T, object?>> key)
 184    {
 0185        _groupingBuilder.Add(key);
 0186        return this;
 187    }
 188
 189    /// <summary>
 190    /// Adds a secondary grouping key using a fluent ThenBy style.
 191    /// </summary>
 192    /// <param name="key">A lambda expression that defines the secondary grouping key.</param>
 193    /// <returns>The current instance of the <see cref="ExtendedCollectionBuilder{T}"/> class.</returns>
 194    public ExtendedCollectionBuilder<T> ThenGroupBy(Expression<Func<T, object?>> key)
 195    {
 0196        _groupingBuilder.ThenBy(key);
 0197        return this;
 198    }
 199
 200    /// <summary>
 201    /// Builds the <see cref="ExtendedCollection{T}"/> instance based on the defined source, filter, and sorting configu
 202    /// </summary>
 203    /// <param name="scheduler">The scheduler to control the execution context for collection updates.</param>
 204    /// <returns>The constructed <see cref="ExtendedCollection{T}"/> instance.</returns>
 205    public ExtendedCollection<T> Build(IScheduler? scheduler = null)
 206    {
 3207        var source = _sourceFactory.Invoke();
 208
 3209        var collection = new ExtendedCollection<T>(source, scheduler);
 210
 211        // Apply filter
 3212        var filter = _filterBuilder.Build();
 3213        if (filter is not null)
 3214            collection.SetFilter(filter);
 215
 216        // Apply sorting
 3217        var sorting = _sortingBuilder.Build();
 3218        collection.SetSorting([.. sorting]);
 219
 220        // Apply grouping
 3221        var grouping = _groupingBuilder.Build();
 3222        if (grouping.Count > 0)
 0223            collection.SetGrouping([.. grouping]);
 224
 3225        return collection;
 226    }
 227
 228    /// <summary>
 229    /// Builds a <see cref="SelectableCollection{T}"/> instance based on the defined source, filter, and sorting configu
 230    /// </summary>
 231    /// <param name="selectionMode">The selection mode for the collection.</param>
 232    /// <param name="scheduler">The scheduler to control the execution context for collection updates.</param>
 233    /// <returns>The constructed <see cref="SelectableCollection{T}"/> instance.</returns>
 234    public SelectableCollection<T> BuildSelectable(SelectionMode selectionMode = SelectionMode.Multiple, IScheduler? sch
 235    {
 6236        var source = _sourceFactory.Invoke();
 237
 6238        var collection = new ExtendedCollection<T>(source, scheduler);
 239
 240        // Apply filter
 6241        var filter = _filterBuilder.Build();
 6242        if (filter is not null)
 0243            collection.SetFilter(filter);
 244
 245        // Apply sorting
 6246        var sorting = _sortingBuilder.Build();
 6247        collection.SetSorting([.. sorting]);
 248
 249        // Apply grouping
 6250        var grouping = _groupingBuilder.Build();
 6251        if (grouping.Count > 0)
 0252            collection.SetGrouping([.. grouping]);
 253
 6254        return new(collection, selectionMode, disposeCollection: true);
 255    }
 256}
 257