Skip to content

Commit

Permalink
Evolve null-checking approach
Browse files Browse the repository at this point in the history
Part of #19233

Removes a lot of null checks from code that is not intended to be called directly by application code.
  • Loading branch information
ajcvickers committed Oct 25, 2021
1 parent cc53b3e commit 3377d77
Show file tree
Hide file tree
Showing 59 changed files with 60 additions and 594 deletions.
7 changes: 1 addition & 6 deletions src/EFCore.Abstractions/ObservableCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore
{
Expand All @@ -25,10 +24,6 @@ public static class ObservableCollectionExtensions
/// <returns>The binding list.</returns>
public static BindingList<T> ToBindingList<T>(this ObservableCollection<T> source)
where T : class
{
Check.NotNull(source, nameof(source));

return new ObservableBackedBindingList<T>(source);
}
=> new ObservableBackedBindingList<T>(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

// ReSharper disable once CheckNamespace
Expand All @@ -32,8 +31,6 @@ public static CosmosClient GetCosmosClient(this DatabaseFacade databaseFacade)

private static TService GetService<TService>(IInfrastructure<IServiceProvider> databaseFacade)
{
Check.NotNull(databaseFacade, nameof(databaseFacade));

var service = databaseFacade.Instance.GetService<TService>();
if (service == null)
{
Expand Down
53 changes: 9 additions & 44 deletions src/EFCore.Cosmos/Extensions/CosmosEntityTypeBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public static EntityTypeBuilder ToContainer(
this EntityTypeBuilder entityTypeBuilder,
string? name)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NullButNotEmpty(name, nameof(name));

entityTypeBuilder.Metadata.SetContainer(name);
Expand Down Expand Up @@ -108,7 +107,6 @@ public static bool CanSetContainer(
string? name,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NullButNotEmpty(name, nameof(name));

return entityTypeBuilder.CanSetAnnotation(CosmosAnnotationNames.ContainerName, name, fromDataAnnotation);
Expand Down Expand Up @@ -200,7 +198,6 @@ public static bool CanSetJsonProperty(
string? name,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NullButNotEmpty(name, nameof(name));

return entityTypeBuilder.CanSetAnnotation(CosmosAnnotationNames.PropertyName, name, fromDataAnnotation);
Expand Down Expand Up @@ -239,11 +236,7 @@ public static EntityTypeBuilder<TEntity> HasPartitionKey<TEntity>(
this EntityTypeBuilder<TEntity> entityTypeBuilder,
string? name)
where TEntity : class
{
entityTypeBuilder.Metadata.SetPartitionKeyPropertyName(name);

return entityTypeBuilder;
}
=> (EntityTypeBuilder<TEntity>)HasPartitionKey((EntityTypeBuilder)entityTypeBuilder, name);

/// <summary>
/// Configures the property that is used to store the partition key.
Expand All @@ -262,9 +255,7 @@ public static EntityTypeBuilder<TEntity> HasPartitionKey<TEntity, TProperty>(
{
Check.NotNull(propertyExpression, nameof(propertyExpression));

entityTypeBuilder.Metadata.SetPartitionKeyPropertyName(propertyExpression.GetMemberAccess().GetSimpleMemberName());

return entityTypeBuilder;
return HasPartitionKey(entityTypeBuilder, propertyExpression.GetMemberAccess().GetSimpleMemberName());
}

/// <summary>
Expand Down Expand Up @@ -313,7 +304,6 @@ public static bool CanSetPartitionKey(
string? name,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NullButNotEmpty(name, nameof(name));

return entityTypeBuilder.CanSetAnnotation(CosmosAnnotationNames.PartitionKeyName, name, fromDataAnnotation);
Expand All @@ -330,11 +320,10 @@ public static bool CanSetPartitionKey(
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static EntityTypeBuilder UseETagConcurrency(this EntityTypeBuilder entityTypeBuilder)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

entityTypeBuilder.Property<string>("_etag")
.ValueGeneratedOnAddOrUpdate()
.IsConcurrencyToken();

return entityTypeBuilder;
}

Expand All @@ -349,11 +338,7 @@ public static EntityTypeBuilder UseETagConcurrency(this EntityTypeBuilder entity
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static EntityTypeBuilder<TEntity> UseETagConcurrency<TEntity>(this EntityTypeBuilder<TEntity> entityTypeBuilder)
where TEntity : class
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
UseETagConcurrency((EntityTypeBuilder)entityTypeBuilder);
return entityTypeBuilder;
}
=> (EntityTypeBuilder<TEntity>)UseETagConcurrency((EntityTypeBuilder)entityTypeBuilder);

/// <summary>
/// Configures the time to live for analytical store in seconds at container scope.
Expand Down Expand Up @@ -388,11 +373,7 @@ public static EntityTypeBuilder<TEntity> HasAnalyticalStoreTimeToLive<TEntity>(
this EntityTypeBuilder<TEntity> entityTypeBuilder,
int? seconds)
where TEntity : class
{
entityTypeBuilder.Metadata.SetAnalyticalStoreTimeToLive(seconds);

return entityTypeBuilder;
}
=> (EntityTypeBuilder<TEntity>)HasAnalyticalStoreTimeToLive((EntityTypeBuilder)entityTypeBuilder, seconds);

/// <summary>
/// Configures the time to live for analytical store in seconds at container scope.
Expand Down Expand Up @@ -478,11 +459,7 @@ public static EntityTypeBuilder<TEntity> HasDefaultTimeToLive<TEntity>(
this EntityTypeBuilder<TEntity> entityTypeBuilder,
int? seconds)
where TEntity : class
{
entityTypeBuilder.Metadata.SetDefaultTimeToLive(seconds);

return entityTypeBuilder;
}
=> (EntityTypeBuilder<TEntity>)HasDefaultTimeToLive((EntityTypeBuilder)entityTypeBuilder, seconds);

/// <summary>
/// Configures the default time to live in seconds at container scope.
Expand Down Expand Up @@ -529,11 +506,7 @@ public static bool CanSetDefaultTimeToLive(
this IConventionEntityTypeBuilder entityTypeBuilder,
int? seconds,
bool fromDataAnnotation = false)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));

return entityTypeBuilder.CanSetAnnotation(CosmosAnnotationNames.DefaultTimeToLive, seconds, fromDataAnnotation);
}
=> entityTypeBuilder.CanSetAnnotation(CosmosAnnotationNames.DefaultTimeToLive, seconds, fromDataAnnotation);

/// <summary>
/// Configures the manual provisioned throughput offering.
Expand Down Expand Up @@ -564,11 +537,7 @@ public static EntityTypeBuilder<TEntity> HasManualThroughput<TEntity>(
this EntityTypeBuilder<TEntity> entityTypeBuilder,
int? throughput)
where TEntity : class
{
entityTypeBuilder.Metadata.SetThroughput(throughput, autoscale: false);

return entityTypeBuilder;
}
=> (EntityTypeBuilder<TEntity>)HasManualThroughput((EntityTypeBuilder)entityTypeBuilder, throughput);

/// <summary>
/// Configures the autoscale provisioned throughput offering.
Expand Down Expand Up @@ -599,11 +568,7 @@ public static EntityTypeBuilder<TEntity> HasAutoscaleThroughput<TEntity>(
this EntityTypeBuilder<TEntity> entityTypeBuilder,
int? throughput)
where TEntity : class
{
entityTypeBuilder.Metadata.SetThroughput(throughput, autoscale: true);

return entityTypeBuilder;
}
=> (EntityTypeBuilder<TEntity>)HasAutoscaleThroughput((EntityTypeBuilder)entityTypeBuilder, throughput);

/// <summary>
/// Configures the provisioned throughput.
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Cosmos/Extensions/CosmosEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ public static void SetETagPropertyName(
/// <returns>The property mapped to ETag, or <see langword="null" /> if no property is mapped to ETag.</returns>
public static IReadOnlyProperty? GetETagProperty(this IReadOnlyEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));
var etagPropertyName = entityType.GetETagPropertyName();

return !string.IsNullOrEmpty(etagPropertyName) ? entityType.FindProperty(etagPropertyName) : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public static ModelBuilder HasDefaultContainer(
this ModelBuilder modelBuilder,
string? name)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NullButNotEmpty(name, nameof(name));

modelBuilder.Model.SetDefaultContainer(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public static PropertyBuilder ToJsonProperty(
this PropertyBuilder propertyBuilder,
string name)
{
Check.NotNull(propertyBuilder, nameof(propertyBuilder));
Check.NotNull(name, nameof(name));

propertyBuilder.Metadata.SetJsonPropertyName(name);
Expand Down Expand Up @@ -118,11 +117,11 @@ public static bool CanSetJsonProperty(
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder IsETagConcurrency(this PropertyBuilder propertyBuilder)
{
Check.NotNull(propertyBuilder, nameof(propertyBuilder));
propertyBuilder
.IsConcurrencyToken()
.ToJsonProperty("_etag")
.ValueGeneratedOnAddOrUpdate();

return propertyBuilder;
}

Expand Down
2 changes: 0 additions & 2 deletions src/EFCore.Cosmos/Extensions/CosmosQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static IQueryable<TEntity> WithPartitionKey<TEntity>(
[NotParameterized] string partitionKey)
where TEntity : class
{
Check.NotNull(source, nameof(source));
Check.NotNull(partitionKey, nameof(partitionKey));

return
Expand Down Expand Up @@ -90,7 +89,6 @@ public static IQueryable<TEntity> FromSqlRaw<TEntity>(
params object[] parameters)
where TEntity : class
{
Check.NotNull(source, nameof(source));
Check.NotEmpty(sql, nameof(sql));
Check.NotNull(parameters, nameof(parameters));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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 @@ -70,17 +69,12 @@ public static IServiceCollection AddCosmos<TContext>(
Action<CosmosDbContextOptionsBuilder>? cosmosOptionsAction = null,
Action<DbContextOptionsBuilder>? optionsAction = null)
where TContext : DbContext
{
Check.NotNull(serviceCollection, nameof(serviceCollection));
Check.NotEmpty(databaseName, nameof(databaseName));

return serviceCollection.AddDbContext<TContext>(
=> serviceCollection.AddDbContext<TContext>(
(serviceProvider, options) =>
{
optionsAction?.Invoke(options);
options.UseCosmos(connectionString, databaseName, cosmosOptionsAction);
});
}

/// <summary>
/// <para>
Expand All @@ -106,8 +100,6 @@ public static IServiceCollection AddCosmos<TContext>(
[EditorBrowsable(EditorBrowsableState.Never)]
public static IServiceCollection AddEntityFrameworkCosmos(this IServiceCollection serviceCollection)
{
Check.NotNull(serviceCollection, nameof(serviceCollection));

var builder = new EntityFrameworkServicesBuilder(serviceCollection)
.TryAdd<LoggingDefinitions, CosmosLoggingDefinitions>()
.TryAdd<IDatabaseProvider, DatabaseProvider<CosmosOptionsExtension>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public class CosmosDbContextOptionsBuilder : ICosmosDbContextOptionsBuilderInfra
/// <param name="optionsBuilder">The options builder.</param>
public CosmosDbContextOptionsBuilder(DbContextOptionsBuilder optionsBuilder)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));

_optionsBuilder = optionsBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal
Expand Down Expand Up @@ -614,8 +613,6 @@ public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo

public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
{
Check.NotNull(debugInfo, nameof(debugInfo));

if (!string.IsNullOrEmpty(Extension._connectionString))
{
debugInfo["Cosmos:" + nameof(ConnectionString)] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
{
Expand Down Expand Up @@ -38,11 +37,6 @@ public ContextContainerConvention(ProviderConventionSetBuilderDependencies depen
public virtual void ProcessModelInitialized(
IConventionModelBuilder modelBuilder,
IConventionContext<IConventionModelBuilder> context)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotNull(context, nameof(context));

modelBuilder.HasDefaultContainer(Dependencies.ContextType.Name);
}
=> modelBuilder.HasDefaultContainer(Dependencies.ContextType.Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
{
Expand Down Expand Up @@ -45,10 +44,6 @@ public virtual void ProcessEntityTypeAnnotationChanged(
IConventionAnnotation? oldAnnotation,
IConventionContext<IConventionAnnotation> context)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NotEmpty(name, nameof(name));
Check.NotNull(context, nameof(context));

if (name == CosmosAnnotationNames.PartitionKeyName)
{
TryConfigurePrimaryKey(entityTypeBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
{
Expand Down Expand Up @@ -46,10 +45,6 @@ public virtual void ProcessEntityTypeAnnotationChanged(
IConventionAnnotation? oldAnnotation,
IConventionContext<IConventionAnnotation> context)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NotEmpty(name, nameof(name));
Check.NotNull(context, nameof(context));

if (name == CosmosAnnotationNames.PartitionKeyName
|| name == CosmosAnnotationNames.ContainerName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
Expand Down Expand Up @@ -41,10 +40,6 @@ public CosmosDateTimeMemberTranslator(ISqlExpressionFactory sqlExpressionFactory
Type returnType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(member, nameof(member));
Check.NotNull(returnType, nameof(returnType));
Check.NotNull(logger, nameof(logger));

var declaringType = member.DeclaringType;
if ((declaringType == typeof(DateTime)
|| declaringType == typeof(DateTimeOffset))
Expand Down
Loading

0 comments on commit 3377d77

Please sign in to comment.