Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Collections.Generic;
11 : using System.Linq;
12 :
13 : namespace Cqrs.Repositories.Queries
14 : {
15 : /// <summary>
16 : /// A collection of extension methods for working with queries.
17 : /// </summary>
18 : public static class QueryPredicateExtensions
19 1 : {
20 : /// <summary>
21 : /// Gets the value from the provided <paramref name="keyValuePair"/> cast to <typeparamref name="TResult"/>.
22 : /// </summary>
23 : /// <remarks>
24 : /// Gets the <see cref="KeyValuePair{TKey,TValue}.Value"/> from the provided <paramref name="keyValuePair"/> cast to <typeparamref name="TResult"/>.
25 : /// </remarks>
26 1 : public static TResult GetValue<TResult>(this KeyValuePair<string, object> keyValuePair)
27 : {
28 : return (TResult) keyValuePair.Value;
29 : }
30 :
31 : /// <summary>
32 : /// Gets the value from the provided <paramref name="collection"/> cast to <typeparamref name="TResult"/>.
33 : /// </summary>
34 : /// <remarks>
35 : /// Filters the provided <paramref name="collection"/> where the <see cref="KeyValuePair{TKey,TValue}.Key"/> equals the provided <paramref name="key"/>
36 : /// then gets the <see cref="KeyValuePair{TKey,TValue}.Value"/> cast to <typeparamref name="TResult"/>.
37 : /// </remarks>
38 1 : public static TResult GetValue<TResult>(this ISet<KeyValuePair<string, object>> collection, string key)
39 : {
40 : return collection.Single(kvp => kvp.Key == key).GetValue<TResult>();
41 : }
42 :
43 : /// <summary>
44 : /// Gets the value from the provided <paramref name="collection"/> cast to <typeparamref name="TResult"/>.
45 : /// </summary>
46 : /// <remarks>
47 : /// Gets the <see cref="KeyValuePair{TKey,TValue}"/> at index <paramref name="index"/> from the provided <paramref name="collection"/>
48 : /// then gets the <see cref="KeyValuePair{TKey,TValue}.Value"/> cast to <typeparamref name="TResult"/>.
49 : /// </remarks>
50 1 : public static TResult GetValue<TResult>(this SortedSet<KeyValuePair<string, object>> collection, int index)
51 : {
52 : int collectionIndex = 0;
53 : foreach (KeyValuePair<string, object> keyValuePair in collection)
54 : {
55 : if (index < collectionIndex++)
56 : continue;
57 : return keyValuePair.GetValue<TResult>();
58 : }
59 : throw new IndexOutOfRangeException();
60 : }
61 :
62 : /// <summary>
63 : /// Gets the value from the provided <paramref name="queryParameter"/> cast to <typeparamref name="TResult"/>.
64 : /// </summary>
65 : /// <remarks>
66 : /// Gets the <see cref="QueryParameter.ParameterValue"/> cast to <typeparamref name="TResult"/>.
67 : /// </remarks>
68 1 : public static TResult GetValue<TResult>(this QueryParameter queryParameter)
69 : {
70 : return (TResult)queryParameter.ParameterValue;
71 : }
72 :
73 : /// <summary>
74 : /// Gets the value from the provided <paramref name="collection"/> cast to <typeparamref name="TResult"/>.
75 : /// </summary>
76 : /// <remarks>
77 : /// Filters the provided <paramref name="collection"/> where the <see cref="QueryParameter.ParameterName"/> equals the provided <paramref name="parameterName"/>
78 : /// then gets the <see cref="QueryParameter.ParameterValue"/> cast to <typeparamref name="TResult"/>.
79 : /// </remarks>
80 1 : public static TResult GetValue<TResult>(this ISet<QueryParameter> collection, string parameterName)
81 : {
82 : return collection.Single(queryParameter => queryParameter.ParameterName == parameterName).GetValue<TResult>();
83 : }
84 :
85 : /// <summary>
86 : /// Gets the value from the provided <paramref name="collection"/> cast to <typeparamref name="TResult"/>.
87 : /// </summary>
88 : /// <remarks>
89 : /// Gets the <see cref="QueryParameter"/> at index <paramref name="index"/> from the provided <paramref name="collection"/>
90 : /// then gets the <see cref="QueryParameter.ParameterValue"/> cast to <typeparamref name="TResult"/>.
91 : /// </remarks>
92 1 : public static TResult GetValue<TResult>(this SortedSet<QueryParameter> collection, int index)
93 : {
94 : int collectionIndex = 0;
95 : foreach (QueryParameter queryParameter in collection)
96 : {
97 : if (index < collectionIndex++)
98 : continue;
99 : return queryParameter.GetValue<TResult>();
100 : }
101 : throw new IndexOutOfRangeException();
102 : }
103 :
104 : /// <summary>
105 : /// Gets the value from the provided <paramref name="collection"/> cast to <typeparamref name="TResult"/>.
106 : /// </summary>
107 : /// <remarks>
108 : /// Filters the provided <paramref name="collection"/> where the <see cref="QueryParameter.ParameterName"/> equals the provided <paramref name="parameterName"/>
109 : /// then gets the <see cref="QueryParameter.ParameterValue"/> cast to <typeparamref name="TResult"/>.
110 : /// </remarks>
111 1 : public static TResult GetValue<TResult>(this SortedSet<QueryParameter> collection, string parameterName)
112 : {
113 : return collection.Single(queryParameter => queryParameter.ParameterName == parameterName).GetValue<TResult>();
114 : }
115 : }
116 : }
|