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.Reflection;
12 :
13 : namespace Cqrs.Repositories.Queries
14 : {
15 : /// <summary>
16 : /// A specification for a query to execute.
17 : /// </summary>
18 : public abstract class QueryStrategy : IQueryStrategy
19 1 : {
20 : #region Implementation of IQueryStrategy
21 :
22 : /// <summary>
23 : /// The predicate that will be evaluated.
24 : /// </summary>
25 : public IQueryPredicate QueryPredicate { get; protected set; }
26 :
27 : #endregion
28 :
29 : /// <summary>
30 : /// Filter to all items not logically deleted.
31 : /// </summary>
32 1 : public virtual IQueryPredicate IsNotLogicallyDeleted()
33 : {
34 : return BuildQueryPredicate(IsNotLogicallyDeleted);
35 : }
36 :
37 : /// <summary>
38 : /// Filter to all items with any permission scope.
39 : /// </summary>
40 1 : public virtual IQueryPredicate WithPermissionScopeAny<TAuthenticationToken>(TAuthenticationToken authenticationToken)
41 : {
42 : return BuildQueryPredicate(WithPermissionScopeAny, authenticationToken);
43 : }
44 :
45 : /// <summary>
46 : /// Filter to any items the authenticated user can view.
47 : /// </summary>
48 1 : public virtual IQueryPredicate WithPermissionScopeUser<TAuthenticationToken>(TAuthenticationToken authenticationToken)
49 : {
50 : return BuildQueryPredicate(WithPermissionScopeUser, authenticationToken);
51 : }
52 :
53 : /// <summary>
54 : /// Filter to any items the company the authenticated user can view.
55 : /// </summary>
56 1 : public virtual IQueryPredicate WithPermissionScopeCompany<TAuthenticationToken>(TAuthenticationToken authenticationToken)
57 : {
58 : return BuildQueryPredicate(WithPermissionScopeCompany, authenticationToken);
59 : }
60 :
61 : /// <summary>
62 : /// Filter to any items the company the authenticated user can view and then filter the results to any items the authenticated user can see.
63 : /// </summary>
64 1 : public virtual IQueryPredicate WithPermissionScopeCompanyAndUser<TAuthenticationToken>(TAuthenticationToken authenticationToken)
65 : {
66 : return BuildQueryPredicate(WithPermissionScopeCompanyAndUser, authenticationToken);
67 : }
68 :
69 : /// <summary>
70 : /// Builds a <see cref="IQueryPredicate"/> from the provided <paramref name="func"/>.
71 : /// </summary>
72 1 : protected virtual IQueryPredicate BuildQueryPredicate<TData>(Func<TData> func)
73 : {
74 : var queryPredicate = new QueryPredicate
75 : {
76 : Name = func.Method.Name,
77 : Parameters = new SortedSet<QueryParameter>()
78 : };
79 :
80 : return queryPredicate;
81 : }
82 :
83 : /// <summary>
84 : /// Builds a <see cref="IQueryPredicate"/> from the provided <paramref name="func"/>
85 : /// storing the provided <paramref name="parameter1"/>.
86 : /// </summary>
87 1 : protected virtual IQueryPredicate BuildQueryPredicate<TParameter1, TData>(Func<TParameter1, TData> func, TParameter1 parameter1)
88 : {
89 : var queryPredicate = new QueryPredicate
90 : {
91 : Name = func.Method.Name,
92 : Parameters = new SortedSet<QueryParameter>()
93 : };
94 :
95 : foreach (ParameterInfo parameterInfo in func.Method.GetParameters())
96 : {
97 : queryPredicate.Parameters.Add(new QueryParameter(parameterInfo.Name, parameter1));
98 : }
99 :
100 : return queryPredicate;
101 : }
102 :
103 : /// <summary>
104 : /// Builds a <see cref="IQueryPredicate"/> from the provided <paramref name="func"/>
105 : /// storing the provided <paramref name="parameter1"/> and <paramref name="parameter2"/>.
106 : /// </summary>
107 1 : protected virtual IQueryPredicate BuildQueryPredicate<TParameter1, TParameter2, TData>(Func<TParameter1, TParameter2, TData> func, TParameter1 parameter1, TParameter2 parameter2)
108 : {
109 : var queryPredicate = new QueryPredicate
110 : {
111 : Name = func.Method.Name,
112 : Parameters = new SortedSet<QueryParameter>()
113 : };
114 :
115 : int parameterIndex = 1;
116 : foreach (ParameterInfo parameterInfo in func.Method.GetParameters())
117 : {
118 : object parameter;
119 : switch (parameterIndex)
120 : {
121 : case 1:
122 : parameter = parameter1;
123 : break;
124 : case 2:
125 : parameter = parameter2;
126 : break;
127 : default:
128 : throw new InvalidOperationException();
129 : }
130 : queryPredicate.Parameters.Add(new QueryParameter(parameterInfo.Name, parameter));
131 : parameterIndex++;
132 : }
133 :
134 : return queryPredicate;
135 : }
136 :
137 : /// <summary>
138 : /// Builds a <see cref="IQueryPredicate"/> from the provided <paramref name="func"/>
139 : /// storing the provided <paramref name="parameter1"/>, <paramref name="parameter2"/> and <paramref name="parameter3"/>.
140 : /// </summary>
141 1 : protected virtual IQueryPredicate BuildQueryPredicate<TParameter1, TParameter2, TParameter3, TData>(Func<TParameter1, TParameter2, TParameter3, TData> func, TParameter1 parameter1, TParameter2 parameter2, TParameter3 parameter3)
142 : {
143 : var queryPredicate = new QueryPredicate
144 : {
145 : Name = func.Method.Name,
146 : Parameters = new SortedSet<QueryParameter>()
147 : };
148 :
149 : int parameterIndex = 1;
150 : foreach (ParameterInfo parameterInfo in func.Method.GetParameters())
151 : {
152 : object parameter;
153 : switch (parameterIndex)
154 : {
155 : case 1:
156 : parameter = parameter1;
157 : break;
158 : case 2:
159 : parameter = parameter2;
160 : break;
161 : case 3:
162 : parameter = parameter3;
163 : break;
164 : default:
165 : throw new InvalidOperationException();
166 : }
167 : queryPredicate.Parameters.Add(new QueryParameter(parameterInfo.Name, parameter));
168 : parameterIndex++;
169 : }
170 :
171 : return queryPredicate;
172 : }
173 :
174 : /// <summary>
175 : /// Builds a <see cref="IQueryPredicate"/> from the provided <paramref name="func"/>
176 : /// storing the provided <paramref name="parameter1"/>, <paramref name="parameter2"/>, <paramref name="parameter3"/> and <paramref name="parameter4"/>.
177 : /// </summary>
178 1 : protected virtual IQueryPredicate BuildQueryPredicate<TParameter1, TParameter2, TParameter3, TParameter4, TData>(Func<TParameter1, TParameter2, TParameter3, TParameter4, TData> func, TParameter1 parameter1, TParameter2 parameter2, TParameter3 parameter3, TParameter4 parameter4)
179 : {
180 : var queryPredicate = new QueryPredicate
181 : {
182 : Name = func.Method.Name,
183 : Parameters = new SortedSet<QueryParameter>()
184 : };
185 :
186 : int parameterIndex = 1;
187 : foreach (ParameterInfo parameterInfo in func.Method.GetParameters())
188 : {
189 : object parameter;
190 : switch (parameterIndex)
191 : {
192 : case 1:
193 : parameter = parameter1;
194 : break;
195 : case 2:
196 : parameter = parameter2;
197 : break;
198 : case 3:
199 : parameter = parameter3;
200 : break;
201 : case 4:
202 : parameter = parameter4;
203 : break;
204 : default:
205 : throw new InvalidOperationException();
206 : }
207 : queryPredicate.Parameters.Add(new QueryParameter(parameterInfo.Name, parameter));
208 : parameterIndex++;
209 : }
210 :
211 : return queryPredicate;
212 : }
213 :
214 : /// <summary>
215 : /// Builds a <see cref="IQueryPredicate"/> from the provided <paramref name="func"/>
216 : /// storing the provided <paramref name="parameter1"/>, <paramref name="parameter2"/>, <paramref name="parameter3"/>, <paramref name="parameter4"/> and <paramref name="parameter5"/>.
217 : /// </summary>
218 1 : protected virtual IQueryPredicate BuildQueryPredicate<TParameter1, TParameter2, TParameter3, TParameter4, TParameter5, TData>(Func<TParameter1, TParameter2, TParameter3, TParameter4, TParameter5, TData> func, TParameter1 parameter1, TParameter2 parameter2, TParameter3 parameter3, TParameter4 parameter4, TParameter5 parameter5)
219 : {
220 : var queryPredicate = new QueryPredicate
221 : {
222 : Name = func.Method.Name,
223 : Parameters = new SortedSet<QueryParameter>()
224 : };
225 :
226 : int parameterIndex = 1;
227 : foreach (ParameterInfo parameterInfo in func.Method.GetParameters())
228 : {
229 : object parameter;
230 : switch (parameterIndex)
231 : {
232 : case 1:
233 : parameter = parameter1;
234 : break;
235 : case 2:
236 : parameter = parameter2;
237 : break;
238 : case 3:
239 : parameter = parameter3;
240 : break;
241 : case 4:
242 : parameter = parameter4;
243 : break;
244 : case 5:
245 : parameter = parameter5;
246 : break;
247 : default:
248 : throw new InvalidOperationException();
249 : }
250 : queryPredicate.Parameters.Add(new QueryParameter(parameterInfo.Name, parameter));
251 : parameterIndex++;
252 : }
253 :
254 : return queryPredicate;
255 : }
256 :
257 : /// <summary>
258 : /// Builds an <see cref="IAndQueryPredicate"/> between <see cref="QueryPredicate"/>
259 : /// and the provided <paramref name="queryPredicate"/>
260 : /// </summary>
261 1 : protected virtual IQueryPredicate And(IQueryPredicate queryPredicate)
262 : {
263 : if (QueryPredicate == null)
264 : return queryPredicate;
265 :
266 : return new AndQueryPredicate
267 : {
268 : LeftQueryPredicate = QueryPredicate,
269 : RightQueryPredicate = queryPredicate
270 : };
271 : }
272 :
273 : /// <summary>
274 : /// Builds an <see cref="IOrQueryPredicate"/> between <see cref="QueryPredicate"/>
275 : /// and the provided <paramref name="queryPredicate"/>
276 : /// </summary>
277 1 : protected virtual IQueryPredicate Or(IQueryPredicate queryPredicate)
278 : {
279 : return new OrQueryPredicate
280 : {
281 : LeftQueryPredicate = QueryPredicate,
282 : RightQueryPredicate = queryPredicate
283 : };
284 : }
285 : }
286 : }
|