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 an <see cref="IAndQueryPredicate"/> between <see cref="QueryPredicate"/>
139 : /// and the provided <paramref name="queryPredicate"/>
140 : /// </summary>
141 1 : protected virtual IQueryPredicate And(IQueryPredicate queryPredicate)
142 : {
143 : if (QueryPredicate == null)
144 : return queryPredicate;
145 :
146 : return new AndQueryPredicate
147 : {
148 : LeftQueryPredicate = QueryPredicate,
149 : RightQueryPredicate = queryPredicate
150 : };
151 : }
152 :
153 : /// <summary>
154 : /// Builds an <see cref="IOrQueryPredicate"/> between <see cref="QueryPredicate"/>
155 : /// and the provided <paramref name="queryPredicate"/>
156 : /// </summary>
157 1 : protected virtual IQueryPredicate Or(IQueryPredicate queryPredicate)
158 : {
159 : return new OrQueryPredicate
160 : {
161 : LeftQueryPredicate = QueryPredicate,
162 : RightQueryPredicate = queryPredicate
163 : };
164 : }
165 : }
166 : }
|