| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SortingBuilder.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.ComponentModel; |
| | | 10 | | using System.Linq.Expressions; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Observable.Collections.Sorting; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides a builder for constructing a list of sorting properties for type T, allowing to define multiple sorting lev |
| | | 16 | | /// </summary> |
| | | 17 | | /// <typeparam name="T">The type of the elements to be sorted.</typeparam> |
| | | 18 | | public class SortingBuilder<T> |
| | | 19 | | { |
| | 18 | 20 | | private readonly List<ISortingProperty<T>> _properties = []; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Creates a new instance of the <see cref="SortingBuilder{T}"/> class, providing a fluent API for constructing a l |
| | | 24 | | /// </summary> |
| | | 25 | | /// <returns>A new instance of the <see cref="SortingBuilder{T}"/> class.</returns> |
| | 18 | 26 | | public static SortingBuilder<T> Create() => new(); |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Adds a sorting property to the builder with the specified expression and sort direction. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="expression">The expression used to select the property to sort by.</param> |
| | | 32 | | /// <param name="direction">The direction in which to sort the property.</param> |
| | | 33 | | /// <returns>The current instance of the <see cref="SortingBuilder{T}"/> for method chaining.</returns> |
| | | 34 | | public SortingBuilder<T> Add(Expression<Func<T, object?>> expression, ListSortDirection direction) |
| | | 35 | | { |
| | 18 | 36 | | _properties.Add(new ExpressionSortingProperty<T>(expression, direction)); |
| | 18 | 37 | | return this; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Adds a sorting property to the builder with the specified expression and ascending sort direction. This method i |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="expression">The expression used to select the property to sort by.</param> |
| | | 44 | | /// <returns>The current instance of the <see cref="SortingBuilder{T}"/> for method chaining.</returns> |
| | | 45 | | public SortingBuilder<T> ThenBy(Expression<Func<T, object?>> expression) |
| | | 46 | | { |
| | 12 | 47 | | Add(expression, ListSortDirection.Ascending); |
| | 12 | 48 | | return this; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Adds a sorting property to the builder with the specified expression and descending sort direction. This method |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="expression">The expression used to select the property to sort by.</param> |
| | | 55 | | /// <returns>The current instance of the <see cref="SortingBuilder{T}"/> for method chaining.</returns> |
| | | 56 | | public SortingBuilder<T> ThenByDescending(Expression<Func<T, object?>> expression) |
| | | 57 | | { |
| | 3 | 58 | | Add(expression, ListSortDirection.Descending); |
| | 3 | 59 | | return this; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Builds the list of sorting properties that have been added to the builder. This method returns a read-only list |
| | | 64 | | /// </summary> |
| | | 65 | | /// <returns>A read-only list of <see cref="ISortingProperty{T}"/> instances representing the sorting configuration. |
| | 18 | 66 | | public IReadOnlyList<ISortingProperty<T>> Build() => _properties; |
| | | 67 | | } |
| | | 68 | | |