| | 1 | | namespace Pozitron.QuerySpecification; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extension methods for specifications. |
| | 5 | | /// </summary> |
| | 6 | | public static class SpecificationExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Creates a new specification by applying a projection specification to the current specification. |
| | 10 | | /// </summary> |
| | 11 | | /// <remarks>This method clones the source specification and applies the projection specification's select |
| | 12 | | /// statements to create a new specification. The input specifications remain unchanged.</remarks> |
| | 13 | | /// <typeparam name="T">The type of the entity.</typeparam> |
| | 14 | | /// <typeparam name="TResult">The type of the result.</typeparam> |
| | 15 | | /// <param name="source">The source specification to which the projection will be applied. Cannot be <see langword=" |
| | 16 | | /// <param name="projectionSpec">The projection specification that defines the transformation to apply to the source |
| | 17 | | /// <see langword="null"/>.</param> |
| | 18 | | /// <returns>A new <see cref="Specification{T, TResult}"/> that represents the result of applying the projection to |
| | 19 | | /// source specification.</returns> |
| | 20 | | public static Specification<T, TResult> WithProjectionOf<T, TResult>(this Specification<T> source, Specification<T, |
| | 21 | | { |
| 3 | 22 | | var newSpec = source.Clone<TResult>(); |
| | 23 | |
|
| 11 | 24 | | foreach (var item in projectionSpec.Items) |
| | 25 | | { |
| 3 | 26 | | if (item.Type == ItemType.Select && item.Reference is not null) |
| | 27 | | { |
| 1 | 28 | | newSpec.AddOrUpdateInternal(item.Type, item.Reference, item.Bag); |
| 1 | 29 | | return newSpec; |
| | 30 | | } |
| | 31 | | } |
| | 32 | |
|
| 2 | 33 | | return newSpec; |
| | 34 | | } |
| | 35 | | } |