Skip to content

Commit

Permalink
Evolve null-checking approach in EFCore.InMemory
Browse files Browse the repository at this point in the history
Part of #19233
  • Loading branch information
ajcvickers committed Nov 2, 2021
1 parent ce751dc commit 94620d7
Show file tree
Hide file tree
Showing 25 changed files with 30 additions and 312 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

[assembly: DesignTimeProviderServices("Microsoft.EntityFrameworkCore.InMemory.Design.Internal.InMemoryDesignTimeServices")]
Expand All @@ -26,9 +25,8 @@ public class InMemoryDesignTimeServices : IDesignTimeServices
/// </summary>
public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
{
Check.NotNull(serviceCollection, nameof(serviceCollection));

serviceCollection.AddEntityFrameworkInMemoryDatabase();

#pragma warning disable EF1001 // Internal EF Core API usage.
new EntityFrameworkDesignServicesBuilder(serviceCollection)
.TryAdd<ICSharpRuntimeAnnotationCodeGenerator, InMemoryCSharpRuntimeAnnotationCodeGenerator>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
Expand All @@ -26,7 +25,7 @@ public static class InMemoryEntityTypeExtensions
public static LambdaExpression? GetInMemoryQuery(this IReadOnlyEntityType entityType)
#pragma warning disable EF1001 // Internal EF Core API usage.
#pragma warning disable CS0612 // Type or member is obsolete
=> (LambdaExpression?)Check.NotNull(entityType, nameof(entityType))[CoreAnnotationNames.DefiningQuery];
=> (LambdaExpression?)entityType[CoreAnnotationNames.DefiningQuery];
#pragma warning restore CS0612 // Type or member is obsolete
#pragma warning restore EF1001 // Internal EF Core API usage.

Expand All @@ -38,7 +37,7 @@ public static class InMemoryEntityTypeExtensions
public static void SetInMemoryQuery(
this IMutableEntityType entityType,
LambdaExpression? inMemoryQuery)
=> Check.NotNull(entityType, nameof(entityType))
=> entityType
#pragma warning disable EF1001 // Internal EF Core API usage.
#pragma warning disable CS0612 // Type or member is obsolete
.SetOrRemoveAnnotation(CoreAnnotationNames.DefiningQuery, inMemoryQuery);
Expand All @@ -56,7 +55,7 @@ public static void SetInMemoryQuery(
this IConventionEntityType entityType,
LambdaExpression? inMemoryQuery,
bool fromDataAnnotation = false)
=> (LambdaExpression?)Check.NotNull(entityType, nameof(entityType))
=> (LambdaExpression?)entityType
#pragma warning disable EF1001 // Internal EF Core API usage.
#pragma warning disable CS0612 // Type or member is obsolete
.SetOrRemoveAnnotation(CoreAnnotationNames.DefiningQuery, inMemoryQuery, fromDataAnnotation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.ValueGeneration;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -43,8 +42,6 @@ public static class InMemoryServiceCollectionExtensions
[EditorBrowsable(EditorBrowsableState.Never)]
public static IServiceCollection AddEntityFrameworkInMemoryDatabase(this IServiceCollection serviceCollection)
{
Check.NotNull(serviceCollection, nameof(serviceCollection));

var builder = new EntityFrameworkServicesBuilder(serviceCollection)
.TryAdd<LoggingDefinitions, InMemoryLoggingDefinitions>()
.TryAdd<IDatabaseProvider, DatabaseProvider<InMemoryOptionsExtension>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.ComponentModel;
using Microsoft.EntityFrameworkCore.InMemory.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Infrastructure
{
Expand All @@ -30,8 +29,6 @@ public class InMemoryDbContextOptionsBuilder : IInMemoryDbContextOptionsBuilderI
/// <param name="optionsBuilder">The options builder.</param>
public InMemoryDbContextOptionsBuilder(DbContextOptionsBuilder optionsBuilder)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));

OptionsBuilder = optionsBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.InMemory.Infrastructure.Internal
{
Expand Down Expand Up @@ -50,8 +49,6 @@ protected virtual void ValidateDefiningQuery(
IModel model,
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
{
Check.NotNull(model, nameof(model));

foreach (var entityType in model.GetEntityTypes())
{
if (entityType.GetInMemoryQuery() != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.InMemory.Query.Internal
{
Expand All @@ -31,9 +30,6 @@ public CollectionResultShaperExpression(
INavigationBase? navigation,
Type elementType)
{
Check.NotNull(projection, nameof(projection));
Check.NotNull(innerShaper, nameof(innerShaper));

Projection = projection;
InnerShaper = innerShaper;
Navigation = navigation;
Expand Down Expand Up @@ -83,8 +79,6 @@ public override Type Type
/// <inheritdoc />
protected override Expression VisitChildren(ExpressionVisitor visitor)
{
Check.NotNull(visitor, nameof(visitor));

var projection = visitor.Visit(Projection);
var innerShaper = visitor.Visit(InnerShaper);

Expand All @@ -100,20 +94,13 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
public virtual CollectionResultShaperExpression Update(
Expression projection,
Expression innerShaper)
{
Check.NotNull(projection, nameof(projection));
Check.NotNull(innerShaper, nameof(innerShaper));

return projection != Projection || innerShaper != InnerShaper
=> projection != Projection || innerShaper != InnerShaper
? new CollectionResultShaperExpression(projection, innerShaper, Navigation, ElementType)
: this;
}

/// <inheritdoc />
void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)
{
Check.NotNull(expressionPrinter, nameof(expressionPrinter));

expressionPrinter.AppendLine("CollectionResultShaperExpression:");
using (expressionPrinter.Indent())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.EntityFrameworkCore.InMemory.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.InMemory.Query.Internal
{
Expand Down Expand Up @@ -177,8 +176,6 @@ public virtual EntityProjectionExpression Clone()
/// </summary>
void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)
{
Check.NotNull(expressionPrinter, nameof(expressionPrinter));

expressionPrinter.AppendLine(nameof(EntityProjectionExpression) + ":");
using (expressionPrinter.Indent())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ public InMemoryExpressionTranslatingExpressionVisitor(
/// </summary>
protected virtual void AddTranslationErrorDetails(string details)
{
Check.NotNull(details, nameof(details));

if (TranslationErrorDetails == null)
{
TranslationErrorDetails = details;
Expand All @@ -127,8 +125,6 @@ protected virtual void AddTranslationErrorDetails(string details)
/// </summary>
public virtual Expression? Translate(Expression expression)
{
Check.NotNull(expression, nameof(expression));

TranslationErrorDetails = null;

return TranslateInternal(expression);
Expand All @@ -152,8 +148,6 @@ protected virtual void AddTranslationErrorDetails(string details)
/// </summary>
protected override Expression VisitBinary(BinaryExpression binaryExpression)
{
Check.NotNull(binaryExpression, nameof(binaryExpression));

if (binaryExpression.Left.Type == typeof(object[])
&& binaryExpression.Left is NewArrayExpression
&& binaryExpression.NodeType == ExpressionType.Equal)
Expand Down Expand Up @@ -229,8 +223,6 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
/// </summary>
protected override Expression VisitConditional(ConditionalExpression conditionalExpression)
{
Check.NotNull(conditionalExpression, nameof(conditionalExpression));

var test = Visit(conditionalExpression.Test);
var ifTrue = Visit(conditionalExpression.IfTrue);
var ifFalse = Visit(conditionalExpression.IfFalse);
Expand Down Expand Up @@ -265,8 +257,6 @@ protected override Expression VisitConditional(ConditionalExpression conditional
/// </summary>
protected override Expression VisitExtension(Expression extensionExpression)
{
Check.NotNull(extensionExpression, nameof(extensionExpression));

switch (extensionExpression)
{
case EntityProjectionExpression _:
Expand Down Expand Up @@ -320,8 +310,6 @@ protected override Expression VisitListInit(ListInitExpression listInitExpressio
/// </summary>
protected override Expression VisitMember(MemberExpression memberExpression)
{
Check.NotNull(memberExpression, nameof(memberExpression));

var innerExpression = Visit(memberExpression.Expression);

// when visiting unary we remove converts from nullable to non-nullable
Expand Down Expand Up @@ -397,8 +385,6 @@ protected override MemberAssignment VisitMemberAssignment(MemberAssignment membe
/// </summary>
protected override Expression VisitMemberInit(MemberInitExpression memberInitExpression)
{
Check.NotNull(memberInitExpression, nameof(memberInitExpression));

var newExpression = Visit(memberInitExpression.NewExpression);
if (newExpression == QueryCompilationContext.NotTranslatedExpression)
{
Expand Down Expand Up @@ -433,8 +419,6 @@ protected override Expression VisitMemberInit(MemberInitExpression memberInitExp
/// </summary>
protected override Expression VisitMethodCall(MethodCallExpression methodCallExpression)
{
Check.NotNull(methodCallExpression, nameof(methodCallExpression));

if (methodCallExpression.Method.IsGenericMethod
&& methodCallExpression.Method.GetGenericMethodDefinition() == ExpressionExtensions.ValueBufferTryReadValueMethod)
{
Expand Down Expand Up @@ -721,8 +705,6 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
/// </summary>
protected override Expression VisitNew(NewExpression newExpression)
{
Check.NotNull(newExpression, nameof(newExpression));

var newArguments = new List<Expression>();
foreach (var argument in newExpression.Arguments)
{
Expand Down Expand Up @@ -751,8 +733,6 @@ protected override Expression VisitNew(NewExpression newExpression)
/// </summary>
protected override Expression VisitNewArray(NewArrayExpression newArrayExpression)
{
Check.NotNull(newArrayExpression, nameof(newArrayExpression));

var newExpressions = new List<Expression>();
foreach (var expression in newArrayExpression.Expressions)
{
Expand Down Expand Up @@ -781,8 +761,6 @@ protected override Expression VisitNewArray(NewArrayExpression newArrayExpressio
/// </summary>
protected override Expression VisitParameter(ParameterExpression parameterExpression)
{
Check.NotNull(parameterExpression, nameof(parameterExpression));

if (parameterExpression.Name?.StartsWith(QueryCompilationContext.QueryParameterPrefix, StringComparison.Ordinal) == true)
{
return Expression.Call(
Expand All @@ -802,8 +780,6 @@ protected override Expression VisitParameter(ParameterExpression parameterExpres
/// </summary>
protected override Expression VisitTypeBinary(TypeBinaryExpression typeBinaryExpression)
{
Check.NotNull(typeBinaryExpression, nameof(typeBinaryExpression));

if (typeBinaryExpression.NodeType == ExpressionType.TypeIs
&& Visit(typeBinaryExpression.Expression) is EntityReferenceExpression entityReferenceExpression)
{
Expand Down Expand Up @@ -851,8 +827,6 @@ protected override Expression VisitTypeBinary(TypeBinaryExpression typeBinaryExp
/// </summary>
protected override Expression VisitUnary(UnaryExpression unaryExpression)
{
Check.NotNull(unaryExpression, nameof(unaryExpression));

var newOperand = Visit(unaryExpression.Operand);
if (newOperand == QueryCompilationContext.NotTranslatedExpression)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,6 @@ protected override Expression VisitConditional(ConditionalExpression conditional
/// </summary>
protected override Expression VisitExtension(Expression extensionExpression)
{
Check.NotNull(extensionExpression, nameof(extensionExpression));

if (extensionExpression is EntityShaperExpression entityShaperExpression)
{
EntityProjectionExpression entityProjectionExpression;
Expand Down Expand Up @@ -395,8 +393,6 @@ protected override MemberAssignment VisitMemberAssignment(MemberAssignment membe
/// </summary>
protected override Expression VisitMemberInit(MemberInitExpression memberInitExpression)
{
Check.NotNull(memberInitExpression, nameof(memberInitExpression));

var newExpression = Visit(memberInitExpression.NewExpression);
if (newExpression == QueryCompilationContext.NotTranslatedExpression)
{
Expand Down Expand Up @@ -469,8 +465,6 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
/// </summary>
protected override Expression VisitNew(NewExpression newExpression)
{
Check.NotNull(newExpression, nameof(newExpression));

if (newExpression.Arguments.Count == 0)
{
return newExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,9 @@ public ProjectionIndexRemappingExpressionVisitor(
private sealed class EntityShaperNullableMarkingExpressionVisitor : ExpressionVisitor
{
protected override Expression VisitExtension(Expression extensionExpression)
{
Check.NotNull(extensionExpression, nameof(extensionExpression));

return extensionExpression is EntityShaperExpression entityShaper
=> extensionExpression is EntityShaperExpression entityShaper
? entityShaper.MakeNullable()
: base.VisitExtension(extensionExpression);
}
}

private sealed class QueryExpressionReplacingExpressionVisitor : ExpressionVisitor
Expand Down
2 changes: 0 additions & 2 deletions src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,6 @@ public sealed override ExpressionType NodeType
/// </summary>
void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)
{
Check.NotNull(expressionPrinter, nameof(expressionPrinter));

expressionPrinter.AppendLine(nameof(InMemoryQueryExpression) + ": ");
using (expressionPrinter.Indent())
{
Expand Down
Loading

0 comments on commit 94620d7

Please sign in to comment.