Skip to content

Commit 2225c53

Browse files
Merge pull request #474 from johelvisguzman/issue389
Added internal guard checks to fetch and specification strategies
2 parents ada9b47 + 6e6482d commit 2225c53

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/DotNetToolkit.Repository/Queries/Strategies/FetchQueryStrategy.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,20 @@ IEnumerable<string> IFetchQueryStrategy<T>.PropertyPaths
8484
/// </summary>
8585
/// <param name="path">A lambda expression representing the path to include.</param>
8686
/// <returns>A new <see cref="IFetchQueryStrategy{T}" /> with the defined query path.</returns>
87-
public IFetchQueryStrategy<T> Fetch(Expression<Func<T, object>> path)
87+
public IFetchQueryStrategy<T> Fetch([NotNull] Expression<Func<T, object>> path)
8888
{
89-
return Fetch(path.ToIncludeString());
89+
return Fetch(Guard.NotNull(path, nameof(path)).ToIncludeString());
9090
}
9191

9292
/// <summary>
9393
/// Specifies the related objects to include in the query results.
9494
/// </summary>
9595
/// <param name="path">The dot-separated list of related objects to return in the query results.</param>
9696
/// <returns>A new <see cref="IFetchQueryStrategy{T}" /> with the defined query path.</returns>
97-
public IFetchQueryStrategy<T> Fetch(string path)
97+
public IFetchQueryStrategy<T> Fetch([NotNull] string path)
9898
{
99+
Guard.NotEmpty(path, nameof(path));
100+
99101
if (!_properties.Contains(path))
100102
_properties.Add(path);
101103

src/DotNetToolkit.Repository/Queries/Strategies/SpecificationQueryStrategy.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace DotNetToolkit.Repository.Queries.Strategies
22
{
3+
using JetBrains.Annotations;
34
using System;
45
using System.Linq;
56
using System.Linq.Expressions;
@@ -16,9 +17,9 @@ public class SpecificationQueryStrategy<T> : ISpecificationQueryStrategy<T>
1617
/// Initializes a new instance of the <see cref="SpecificationQueryStrategy{T}" /> class.
1718
/// </summary>
1819
/// <param name="predicate">A function to test the entity and determine if it satisfies the specified criteria.</param>
19-
public SpecificationQueryStrategy(Expression<Func<T, bool>> predicate)
20+
public SpecificationQueryStrategy([NotNull] Expression<Func<T, bool>> predicate)
2021
{
21-
Predicate = predicate;
22+
Predicate = Guard.NotNull(predicate, nameof(predicate));
2223
}
2324

2425
#endregion
@@ -28,33 +29,33 @@ public SpecificationQueryStrategy(Expression<Func<T, bool>> predicate)
2829
/// <summary>
2930
/// Returns a new specificationStrategy which has been combined with the current and the specified specificationStrategy using the logical "and".
3031
/// </summary>
31-
public ISpecificationQueryStrategy<T> And(SpecificationQueryStrategy<T> specificationStrategy)
32+
public ISpecificationQueryStrategy<T> And([NotNull] SpecificationQueryStrategy<T> specificationStrategy)
3233
{
33-
return new SpecificationQueryStrategy<T>(Predicate.And(specificationStrategy.Predicate));
34+
return new SpecificationQueryStrategy<T>(Predicate.And(Guard.NotNull(specificationStrategy, nameof(specificationStrategy)).Predicate));
3435
}
3536

3637
/// <summary>
3738
/// Returns a new specificationStrategy which has been combined with the current specified predicate using the logical "and".
3839
/// </summary>
39-
public ISpecificationQueryStrategy<T> And(Expression<Func<T, bool>> predicate)
40+
public ISpecificationQueryStrategy<T> And([NotNull] Expression<Func<T, bool>> predicate)
4041
{
41-
return new SpecificationQueryStrategy<T>(Predicate.And(predicate));
42+
return new SpecificationQueryStrategy<T>(Predicate.And(Guard.NotNull(predicate, nameof(predicate))));
4243
}
4344

4445
/// <summary>
4546
/// Returns a new specificationStrategy which has been combined with the current and the specified specificationStrategy using the logical "or".
4647
/// </summary>
47-
public ISpecificationQueryStrategy<T> Or(SpecificationQueryStrategy<T> specificationStrategy)
48+
public ISpecificationQueryStrategy<T> Or([NotNull] SpecificationQueryStrategy<T> specificationStrategy)
4849
{
49-
return new SpecificationQueryStrategy<T>(Predicate.Or(specificationStrategy.Predicate));
50+
return new SpecificationQueryStrategy<T>(Predicate.Or(Guard.NotNull(specificationStrategy, nameof(specificationStrategy)).Predicate));
5051
}
5152

5253
/// <summary>
5354
/// Returns a new specificationStrategy which has been combined with the current specified predicate using the logical "or".
5455
/// </summary>
55-
public ISpecificationQueryStrategy<T> Or(Expression<Func<T, bool>> predicate)
56+
public ISpecificationQueryStrategy<T> Or([NotNull] Expression<Func<T, bool>> predicate)
5657
{
57-
return new SpecificationQueryStrategy<T>(Predicate.Or(predicate));
58+
return new SpecificationQueryStrategy<T>(Predicate.Or(Guard.NotNull(predicate, nameof(predicate))));
5859
}
5960

6061
/// <summary>
@@ -79,19 +80,21 @@ public ISpecificationQueryStrategy<T> Not()
7980
/// </summary>
8081
/// <param name="query">The entity query.</param>
8182
/// <returns>The collection of entities that satisfied the criteria specified by the <see cref="ISpecificationQueryStrategy{T}.Predicate" /> from the query.</returns>
82-
public virtual IQueryable<T> SatisfyingEntitiesFrom(IQueryable<T> query)
83+
public virtual IQueryable<T> SatisfyingEntitiesFrom([NotNull] IQueryable<T> query)
8384
{
84-
return Predicate == null ? query : query.Where(Predicate);
85+
return Guard.NotNull(query, nameof(query)).Where(Predicate);
8586
}
8687

8788
/// <summary>
8889
/// Determines wheter the entity that satisfied the criteria specified by the <see cref="ISpecificationQueryStrategy{T}.Predicate" />.
8990
/// </summary>
9091
/// <param name="entity">The entity to test.</param>
9192
/// <returns><c>true</c> if the entity satisfied the criteria specified by the <see cref="ISpecificationQueryStrategy{T}.Predicate" />; otherwise, <c>false</c>.</returns>
92-
public bool IsSatisfiedBy(T entity)
93+
public bool IsSatisfiedBy([NotNull] T entity)
9394
{
94-
return Predicate == null || new[] { entity }.AsQueryable().Any(Predicate);
95+
return new[] { Guard.NotNull(entity, nameof(entity)) }
96+
.AsQueryable()
97+
.Any(Predicate);
9598
}
9699

97100
#endregion

0 commit comments

Comments
 (0)