| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="GroupingBuilder.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.Linq.Expressions; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Observable.Collections.Grouping; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides a builder for constructing a list of grouping properties for type T, allowing to define multiple grouping l |
| | | 15 | | /// </summary> |
| | | 16 | | /// <typeparam name="T">The type of the elements to be grouped.</typeparam> |
| | | 17 | | public class GroupingBuilder<T> |
| | | 18 | | { |
| | 12 | 19 | | private readonly List<IGroupingProperty<T>> _properties = []; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Creates a new instance of the <see cref="GroupingBuilder{T}"/> class, providing a fluent API for constructing a |
| | | 23 | | /// </summary> |
| | | 24 | | /// <returns>A new instance of the <see cref="GroupingBuilder{T}"/> class.</returns> |
| | 12 | 25 | | public static GroupingBuilder<T> Create() => new(); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Adds a grouping property to the builder with the specified expression. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="expression">The expression used to select the property to group by.</param> |
| | | 31 | | /// <returns>The current instance of the <see cref="GroupingBuilder{T}"/> for method chaining.</returns> |
| | | 32 | | public GroupingBuilder<T> Add(Expression<Func<T, object?>> expression) |
| | | 33 | | { |
| | 6 | 34 | | _properties.Add(new ExpressionGroupingProperty<T>(expression)); |
| | 6 | 35 | | return this; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Adds a grouping property to the builder with the specified expression. This method is a convenience method for a |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="expression">The expression used to select the property to group by.</param> |
| | | 42 | | /// <returns>The current instance of the <see cref="GroupingBuilder{T}"/> for method chaining.</returns> |
| | | 43 | | public GroupingBuilder<T> ThenBy(Expression<Func<T, object?>> expression) |
| | | 44 | | { |
| | 6 | 45 | | Add(expression); |
| | 6 | 46 | | return this; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Builds the list of grouping properties that have been added to the builder. This method returns a read-only list |
| | | 51 | | /// </summary> |
| | | 52 | | /// <returns>A read-only list of <see cref="IGroupingProperty{T}"/> instances representing the grouping configuratio |
| | 12 | 53 | | public IReadOnlyList<IGroupingProperty<T>> Build() => _properties; |
| | | 54 | | } |
| | | 55 | | |