forked from SharpRepository/SharpRepository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecification.cs
More file actions
238 lines (196 loc) · 9.08 KB
/
Specification.cs
File metadata and controls
238 lines (196 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.Linq;
using System.Linq.Expressions;
using SharpRepository.Repository.Caching.Hash;
using SharpRepository.Repository.FetchStrategies;
using System.Collections;
using System.Collections.Generic;
namespace SharpRepository.Repository.Specifications
{
/// <summary>
/// In simple terms, a specification is a small piece of logic which is independent and give an answer
/// to the question “does this match?”. With Specification, we isolate the logic that do the selection
/// into a reusable business component that can be passed around easily from the entity we are selecting.
/// </summary>
/// <see cref="http://en.wikipedia.org/wiki/Specification_pattern"/>
///
/// Inspired by Huy Nguyen's article, Entity Framework 4 POCO, Repository and Specification Pattern
/// <see cref="http://huyrua.wordpress.com/2010/07/13/entity-framework-4-poco-repository-and-specification-pattern/"/>
/// and open source project
/// <see cref="http://code.google.com/p/ef4prs/downloads/list"/>
///
/// And Will Beattie's article, Specification Pattern, Entity Framework & LINQ
/// <see cref="http://blog.willbeattie.net/2011/02/specification-pattern-entity-framework.html"/>
///
/// <typeparam name="T"></typeparam>
public class Specification<T> : ISpecification<T>
{
public Specification()
: this((Expression<Func<T, bool>>)null)
{
// null Predicate means it matches everything
}
public Specification(Expression<Func<T, bool>> predicate)
{
Predicate = predicate;
FetchStrategy = new GenericFetchStrategy<T>();
}
public Specification(ISpecification<T> specification)
{
Predicate = specification.Predicate;
FetchStrategy = specification.FetchStrategy;
}
#region ISpecification<T> Members
public Expression<Func<T, bool>> Predicate { get; set; }
public virtual T SatisfyingEntityFrom(IQueryable<T> query)
{
return SatisfyingEntitiesFrom(query).FirstOrDefault();
}
public virtual IQueryable<T> SatisfyingEntitiesFrom(IQueryable<T> query)
{
return Predicate == null ? query : query.Where(Predicate);
}
public bool IsSatisfiedBy(T entity)
{
return Predicate == null || new[] { entity }.AsQueryable().Any(Predicate);
}
public IFetchStrategy<T> FetchStrategy { get; set; }
#endregion
protected virtual Specification<T> Instanciate(Expression<Func<T, bool>> predicate, IFetchStrategy<T> strategy = null)
{
var specification = new Specification<T>(predicate);
if (strategy != null)
specification.FetchStrategy = strategy;
return specification;
}
protected IFetchStrategy<T> InstanciateFetchStrategy(IFetchStrategy<T> strategy)
{
var thisPaths = FetchStrategy != null ? FetchStrategy.IncludePaths : new List<string>();
var paramPaths = strategy != null ? strategy.IncludePaths : new List<string>();
var includePaths = thisPaths.Union(paramPaths);
var newStrategy = new GenericFetchStrategy<T>();
foreach (var includePath in includePaths)
{
newStrategy.Include(includePath);
}
return newStrategy;
}
public ISpecification<T> And(ISpecification<T> specification)
{
return Instanciate(Predicate.And(specification.Predicate), InstanciateFetchStrategy(specification.FetchStrategy) );
}
public ISpecification<T> And(Expression<Func<T, bool>> predicate)
{
return Instanciate(Predicate.And(predicate), FetchStrategy);
}
public ISpecification<T> AndAlso(ISpecification<T> specification)
{
return Instanciate(Predicate.AndAlso(specification.Predicate), InstanciateFetchStrategy(specification.FetchStrategy));
}
public ISpecification<T> AndAlso(Expression<Func<T, bool>> predicate)
{
return Instanciate(Predicate.AndAlso(predicate), FetchStrategy);
}
public ISpecification<T> Not()
{
return Instanciate(Predicate.Not(), FetchStrategy);
}
public ISpecification<T> AndNot(ISpecification<T> specification)
{
return Instanciate(Predicate.AndNot(specification.Predicate), InstanciateFetchStrategy(specification.FetchStrategy));
}
public ISpecification<T> AndNot(Expression<Func<T, bool>> predicate)
{
return Instanciate(Predicate.AndNot(predicate), FetchStrategy);
}
public ISpecification<T> OrNot(ISpecification<T> specification)
{
return Instanciate(Predicate.OrNot(specification.Predicate), InstanciateFetchStrategy(specification.FetchStrategy));
}
public ISpecification<T> OrNot(Expression<Func<T, bool>> predicate)
{
return Instanciate(Predicate.OrNot(predicate), FetchStrategy);
}
public ISpecification<T> Or(ISpecification<T> specification)
{
return Instanciate(Predicate.Or(specification.Predicate), InstanciateFetchStrategy(specification.FetchStrategy));
}
public ISpecification<T> Or(Expression<Func<T, bool>> predicate)
{
return Instanciate(Predicate.Or(predicate), FetchStrategy);
}
public ISpecification<T> OrElse(ISpecification<T> specification)
{
return Instanciate(Predicate.OrElse(specification.Predicate), InstanciateFetchStrategy(specification.FetchStrategy));
}
public ISpecification<T> OrElse(Expression<Func<T, bool>> predicate)
{
return Instanciate(Predicate.OrElse(predicate), FetchStrategy);
}
public static ISpecification<T> And(ISpecification<T> specification, ISpecification<T> specification2)
{
return new Specification<T>(specification.And(specification2));
}
public static ISpecification<T> And(Expression<Func<T, bool>> predicate, Expression<Func<T, bool>> predicate2)
{
return new Specification<T>(predicate.And(predicate2));
}
public static ISpecification<T> AndAlso(ISpecification<T> specification, ISpecification<T> specification2)
{
return new Specification<T>(specification.AndAlso(specification));
}
public static ISpecification<T> AndAlso(Expression<Func<T, bool>> predicate, Expression<Func<T, bool>> predicate2)
{
return new Specification<T>(predicate.AndAlso(predicate2));
}
public static ISpecification<T> Not(ISpecification<T> specification)
{
return new Specification<T>(specification.Not());
}
public static ISpecification<T> AndNot(ISpecification<T> specification, ISpecification<T> specification2)
{
return new Specification<T>(specification.AndNot(specification2));
}
public static ISpecification<T> AndNot(Expression<Func<T, bool>> predicate, Expression<Func<T, bool>> predicate2)
{
return new Specification<T>(predicate.AndNot(predicate2));
}
public static ISpecification<T> OrNot(ISpecification<T> specification, ISpecification<T> specification2)
{
return new Specification<T>(specification.OrNot(specification2));
}
public static ISpecification<T> OrNot(Expression<Func<T, bool>> predicate, Expression<Func<T, bool>> predicate2)
{
return new Specification<T>(predicate.OrNot(predicate2));
}
public static ISpecification<T> Or(ISpecification<T> specification, ISpecification<T> specification2)
{
return new Specification<T>(specification.Or(specification2));
}
public static ISpecification<T> Or(Expression<Func<T, bool>> predicate, Expression<Func<T, bool>> predicate2)
{
return new Specification<T>(predicate.Or(predicate2));
}
public static ISpecification<T> OrElse(ISpecification<T> specification, ISpecification<T> specification2)
{
return new Specification<T>(specification.OrElse(specification2));
}
public static ISpecification<T> OrElse(Expression<Func<T, bool>> predicate, Expression<Func<T, bool>> predicate2)
{
return new Specification<T>(predicate.OrElse(predicate2));
}
/// <summary>
/// Used to get a unique string for this particular specification. The Generational key uses a MD5 of this value.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return String.Format("Specification Type: {0}\nEntity Type: {1}\nPredicate: {2}\nFetchStrategy: {3}",
GetType().Name,
(typeof(T)).Name,
HashGenerator.FromPredicate(Predicate),
FetchStrategy
);
}
}
}