Skip to content

Commit 8e9da2a

Browse files
authored
Fixed nullability warnings. (#223)
1 parent b44367a commit 8e9da2a

File tree

9 files changed

+42
-17
lines changed

9 files changed

+42
-17
lines changed

Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Ardalis.Specification.EntityFrameworkCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2222
<EmbedUntrackedSources>true</EmbedUntrackedSources>
2323
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
24-
<LangVersion>8.0</LangVersion>
24+
<LangVersion>9.0</LangVersion>
2525
<Nullable>enable</Nullable>
2626
</PropertyGroup>
2727

Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/CachedReadConcurrentDictionary.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Concurrent;
44
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Runtime.CompilerServices;
67
using System.Threading;
78

@@ -12,7 +13,7 @@ namespace Ardalis.Specification.EntityFrameworkCore
1213
/// </summary>
1314
/// <typeparam name="TKey">The key type.</typeparam>
1415
/// <typeparam name="TValue">The value type.</typeparam>
15-
internal class CachedReadConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>
16+
internal class CachedReadConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue> where TKey : notnull
1617
{
1718
/// <summary>
1819
/// The number of cache misses which are tolerated before the cache is regenerated.
@@ -181,9 +182,15 @@ public bool Remove(TKey key)
181182
return result;
182183
}
183184

185+
#if NET6_0_OR_GREATER
186+
/// <inheritdoc />
187+
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) => this.GetReadDictionary().TryGetValue(key, out value);
188+
#else
184189
/// <inheritdoc />
185190
public bool TryGetValue(TKey key, out TValue value) => this.GetReadDictionary().TryGetValue(key, out value);
186191

192+
#endif
193+
187194
/// <inheritdoc />
188195
public TValue this[TKey key]
189196
{

Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/IncludeEvaluator.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ private IQueryable<T> BuildInclude<T>(IQueryable query, IncludeExpressionInfo in
8989

9090
if (!this.cacheEnabled)
9191
{
92-
return (IQueryable<T>)IncludeMethodInfo.MakeGenericMethod(includeInfo.EntityType, includeInfo.PropertyType).Invoke(null, new object[] { query, includeInfo.LambdaExpression, });
92+
var result = IncludeMethodInfo.MakeGenericMethod(includeInfo.EntityType, includeInfo.PropertyType).Invoke(null, new object[] { query, includeInfo.LambdaExpression });
93+
94+
_ = result ?? throw new TargetException();
95+
96+
return (IQueryable<T>)result;
9397
}
9498

9599
var include = DelegatesCache.GetOrAdd((includeInfo.EntityType, includeInfo.PropertyType, null), CreateIncludeDelegate).Value;
@@ -104,10 +108,14 @@ private IQueryable<T> BuildThenInclude<T>(IQueryable query, IncludeExpressionInf
104108

105109
if (!this.cacheEnabled)
106110
{
107-
return (IQueryable<T>)(IsGenericEnumerable(includeInfo.PreviousPropertyType, out var previousPropertyType)
111+
var result = (IsGenericEnumerable(includeInfo.PreviousPropertyType, out var previousPropertyType)
108112
? ThenIncludeAfterEnumerableMethodInfo
109113
: ThenIncludeAfterReferenceMethodInfo).MakeGenericMethod(includeInfo.EntityType, previousPropertyType, includeInfo.PropertyType)
110114
.Invoke(null, new object[] { query, includeInfo.LambdaExpression, });
115+
116+
_ = result ?? throw new TargetException();
117+
118+
return (IQueryable<T>)result;
111119
}
112120

113121
var thenInclude = DelegatesCache.GetOrAdd((includeInfo.EntityType, includeInfo.PropertyType, includeInfo.PreviousPropertyType), CreateThenIncludeDelegate).Value;
@@ -144,8 +152,10 @@ private static Lazy<Func<IQueryable, LambdaExpression, IQueryable>> CreateInclud
144152
private static Lazy<Func<IQueryable, LambdaExpression, IQueryable>> CreateThenIncludeDelegate((Type EntityType, Type PropertyType, Type? PreviousPropertyType) cacheKey)
145153
=> new Lazy<Func<IQueryable, LambdaExpression, IQueryable>>(() =>
146154
{
155+
_ = cacheKey.PreviousPropertyType ?? throw new ArgumentNullException(nameof(cacheKey.PreviousPropertyType));
156+
147157
MethodInfo thenIncludeInfo = ThenIncludeAfterReferenceMethodInfo;
148-
if (IsGenericEnumerable(cacheKey.PreviousPropertyType!, out var previousPropertyType))
158+
if (IsGenericEnumerable(cacheKey.PreviousPropertyType, out var previousPropertyType))
149159
{
150160
thenIncludeInfo = ThenIncludeAfterEnumerableMethodInfo;
151161
}

Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/SearchExtension.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
34
using System.Linq;
45
using System.Linq.Expressions;
6+
using System.Reflection;
57
using Microsoft.EntityFrameworkCore;
68

79
namespace Ardalis.Specification.EntityFrameworkCore
810
{
911
public static class SearchExtension
1012
{
13+
private static readonly MethodInfo LikeMethodInfo = typeof(DbFunctionsExtensions)
14+
.GetMethod(nameof(DbFunctionsExtensions.Like), new Type[] { typeof(DbFunctions), typeof(string), typeof(string) })
15+
?? throw new TargetException("The EF.Functions.Like not found");
16+
17+
private static readonly MemberExpression Functions = Expression.Property(null, typeof(EF).GetProperty(nameof(EF.Functions))
18+
?? throw new TargetException("The EF.Functions not found!"));
19+
1120
/// <summary>
1221
/// Filters <paramref name="source"/> by applying an 'SQL LIKE' operation to it.
1322
/// </summary>
@@ -30,16 +39,14 @@ public static IQueryable<T> Search<T>(this IQueryable<T> source, IEnumerable<Sea
3039
if (string.IsNullOrEmpty(criteria.SearchTerm))
3140
continue;
3241

33-
var functions = Expression.Property(null, typeof(EF).GetProperty(nameof(EF.Functions)));
34-
var like = typeof(DbFunctionsExtensions).GetMethod(nameof(DbFunctionsExtensions.Like), new Type[] { functions.Type, typeof(string), typeof(string) });
35-
36-
var propertySelector = ParameterReplacerVisitor.Replace(criteria.Selector, criteria.Selector.Parameters[0], parameter);
42+
var propertySelector = ParameterReplacerVisitor.Replace(criteria.Selector, criteria.Selector.Parameters[0], parameter) as LambdaExpression;
43+
_ = propertySelector ?? throw new InvalidExpressionException();
3744

3845
var likeExpression = Expression.Call(
3946
null,
40-
like,
41-
functions,
42-
(propertySelector as LambdaExpression)?.Body,
47+
LikeMethodInfo,
48+
Functions,
49+
propertySelector.Body,
4350
Expression.Constant(criteria.SearchTerm));
4451

4552
expr = expr == null ? (Expression)likeExpression : Expression.OrElse(expr, likeExpression);

Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationTo
7272
return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken);
7373
}
7474
/// <inheritdoc/>
75-
public virtual async Task<TResult> GetBySpecAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default)
75+
public virtual async Task<TResult?> GetBySpecAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default)
7676
{
7777
return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken);
7878
}

Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>false</IsPackable>
6-
<Nullable>enable</Nullable>
6+
<LangVersion>9.0</LangVersion>
7+
<Nullable>enable</Nullable>
78
</PropertyGroup>
89

910
<ItemGroup>

Specification/src/Ardalis.Specification/Ardalis.Specification.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<PublishRepositoryUrl>true</PublishRepositoryUrl>
3434
<EmbedUntrackedSources>true</EmbedUntrackedSources>
3535
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
36-
<LangVersion>8.0</LangVersion>
36+
<LangVersion>9.0</LangVersion>
3737
<Nullable>enable</Nullable>
3838
</PropertyGroup>
3939

Specification/src/Ardalis.Specification/IReadRepositoryBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public interface IReadRepositoryBase<T> where T : class
4343
/// A task that represents the asynchronous operation.
4444
/// The task result contains the <typeparamref name="TResult" />.
4545
/// </returns>
46-
Task<TResult> GetBySpecAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default);
46+
Task<TResult?> GetBySpecAsync<TResult>(ISpecification<T, TResult> specification, CancellationToken cancellationToken = default);
4747

4848
/// <summary>
4949
/// Finds all entities of <typeparamref name="T" /> from the database.

Specification/tests/Ardalis.Specification.UnitTests/Ardalis.Specification.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net6.0;net472</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
<Nullable>enable</Nullable>
7-
<LangVersion>8.0</LangVersion>
7+
<LangVersion>9.0</LangVersion>
88
</PropertyGroup>
99

1010
<ItemGroup>

0 commit comments

Comments
 (0)