Skip to content

Commit

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

Quite aggressive, based on not expected to be called by application code.
  • Loading branch information
ajcvickers committed Oct 28, 2021
1 parent 90f0808 commit 0af77cc
Show file tree
Hide file tree
Showing 33 changed files with 14 additions and 525 deletions.
3 changes: 0 additions & 3 deletions src/EFCore.Design/Design/DbContextActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Design
{
Expand Down Expand Up @@ -44,8 +43,6 @@ public static DbContext CreateInstance(
IOperationReportHandler? reportHandler,
string[]? args)
{
Check.NotNull(contextType, nameof(contextType));

return new DbContextOperations(
new OperationReporter(reportHandler),
contextType.Assembly,
Expand Down
10 changes: 0 additions & 10 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Design.Internal
{
Expand Down Expand Up @@ -161,9 +160,6 @@ public CSharpHelper(ITypeMappingSource typeMappingSource)
/// </summary>
public virtual string Lambda(IReadOnlyList<string> properties, string? lambdaIdentifier)
{
Check.NotNull(properties, nameof(properties));
Check.NullButNotEmpty(lambdaIdentifier, nameof(lambdaIdentifier));

lambdaIdentifier ??= "x";
var builder = new StringBuilder();
builder.Append(lambdaIdentifier);
Expand Down Expand Up @@ -194,8 +190,6 @@ public virtual string Lambda(IReadOnlyList<string> properties, string? lambdaIde
/// </summary>
public virtual string Reference(Type type, bool? fullName = null)
{
Check.NotNull(type, nameof(type));

fullName ??= type.IsNested ? ShouldUseFullName(type.DeclaringType!) : ShouldUseFullName(type);

return type.DisplayName(fullName.Value, compilable: true);
Expand Down Expand Up @@ -227,8 +221,6 @@ public virtual bool ShouldUseFullName(string shortTypeName)
/// </summary>
public virtual string Identifier(string name, ICollection<string>? scope = null, bool? capitalize = null)
{
Check.NotEmpty(name, nameof(name));

var builder = new StringBuilder();
var partStart = 0;

Expand Down Expand Up @@ -305,8 +297,6 @@ private static StringBuilder ChangeFirstLetterCase(StringBuilder builder, bool c
/// </summary>
public virtual string Namespace(params string[] name)
{
Check.NotNull(name, nameof(name));

var @namespace = new StringBuilder();
foreach (var piece in name.Where(p => !string.IsNullOrEmpty(p))
.SelectMany(p => p.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries)))
Expand Down
10 changes: 0 additions & 10 deletions src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.IO;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Design.Internal
Expand Down Expand Up @@ -43,10 +42,6 @@ public DatabaseOperations(
bool nullable,
string[]? args)
{
Check.NotNull(reporter, nameof(reporter));
Check.NotNull(startupAssembly, nameof(startupAssembly));
Check.NotNull(projectDir, nameof(projectDir));

_reporter = reporter;
_projectDir = projectDir;
_rootNamespace = rootNamespace;
Expand Down Expand Up @@ -79,11 +74,6 @@ public virtual SavedModelFiles ScaffoldContext(
bool suppressOnConfiguring,
bool noPluralize)
{
Check.NotEmpty(provider, nameof(provider));
Check.NotEmpty(connectionString, nameof(connectionString));
Check.NotNull(schemas, nameof(schemas));
Check.NotNull(tables, nameof(tables));

outputDir = outputDir != null
? Path.GetFullPath(Path.Combine(_projectDir, outputDir))
: _projectDir;
Expand Down
5 changes: 0 additions & 5 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ protected DbContextOperations(
string[]? args,
AppServiceProviderFactory appServicesFactory)
{
Check.NotNull(reporter, nameof(reporter));
Check.NotNull(assembly, nameof(assembly));
Check.NotNull(startupAssembly, nameof(startupAssembly));
Check.NotNull(projectDir, nameof(projectDir));

_reporter = reporter;
_assembly = assembly;
_startupAssembly = startupAssembly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public DesignTimeServicesBuilder(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual IServiceProvider Build(DbContext context)
=> CreateServiceCollection(Check.NotNull(context, nameof(context))).BuildServiceProvider();
=> CreateServiceCollection(context).BuildServiceProvider();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -78,7 +78,7 @@ public virtual IServiceCollection CreateServiceCollection(DbContext context)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual IServiceProvider Build(string provider)
=> CreateServiceCollection(Check.NotEmpty(provider, nameof(provider))).BuildServiceProvider();
=> CreateServiceCollection(provider).BuildServiceProvider();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
8 changes: 0 additions & 8 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Design;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Design.Internal
Expand Down Expand Up @@ -50,11 +49,6 @@ public MigrationsOperations(
bool nullable,
string[]? args)
{
Check.NotNull(reporter, nameof(reporter));
Check.NotNull(assembly, nameof(assembly));
Check.NotNull(startupAssembly, nameof(startupAssembly));
Check.NotNull(projectDir, nameof(projectDir));

_reporter = reporter;
_assembly = assembly;
_projectDir = projectDir;
Expand Down Expand Up @@ -87,8 +81,6 @@ public virtual MigrationFiles AddMigration(
string? contextType,
string? @namespace)
{
Check.NotEmpty(name, nameof(name));

if (outputDir != null)
{
outputDir = Path.GetFullPath(Path.Combine(_projectDir, outputDir));
Expand Down
3 changes: 0 additions & 3 deletions src/EFCore.Design/Design/Internal/OperationLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.Logging;

namespace Microsoft.EntityFrameworkCore.Design.Internal
Expand All @@ -24,8 +23,6 @@ public class OperationLoggerProvider : ILoggerProvider
/// </summary>
public OperationLoggerProvider(IOperationReporter reporter)
{
Check.NotNull(reporter, nameof(reporter));

_reporter = reporter;
}

Expand Down
16 changes: 2 additions & 14 deletions src/EFCore.Design/Design/OperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,6 @@ public abstract class OperationBase : MarshalByRefObject
/// <param name="resultHandler">The <see cref="IOperationResultHandler" />.</param>
protected OperationBase(IOperationResultHandler resultHandler)
{
Check.NotNull(resultHandler, nameof(resultHandler));

_resultHandler = resultHandler;
}

Expand All @@ -714,8 +712,6 @@ protected OperationBase(IOperationResultHandler resultHandler)
/// <param name="action">The action to execute.</param>
protected virtual void Execute(Action action)
{
Check.NotNull(action, nameof(action));

try
{
action();
Expand All @@ -732,23 +728,15 @@ protected virtual void Execute(Action action)
/// <typeparam name="T">The result type.</typeparam>
/// <param name="action">The action to execute.</param>
protected virtual void Execute<T>(Func<T> action)
{
Check.NotNull(action, nameof(action));

Execute(() => _resultHandler.OnResult(action()));
}
=> Execute(() => _resultHandler.OnResult(action()));

/// <summary>
/// Executes an action passing results or exceptions to the <see cref="IOperationResultHandler" />.
/// </summary>
/// <typeparam name="T">The type of results.</typeparam>
/// <param name="action">The action to execute.</param>
protected virtual void Execute<T>(Func<IEnumerable<T>> action)
{
Check.NotNull(action, nameof(action));

Execute(() => _resultHandler.OnResult(action().ToArray()));
}
=> Execute(() => _resultHandler.OnResult(action().ToArray()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal
{
/// <summary>
Expand Down Expand Up @@ -32,6 +30,6 @@ public static string GetDbSetName(this IReadOnlyEntityType entityType)
public static void SetDbSetName(this IMutableEntityType entityType, string? value)
=> entityType.SetAnnotation(
ScaffoldingAnnotationNames.DbSetName,
Check.NullButNotEmpty(value, nameof(value)));
value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata.Internal
{
Expand Down Expand Up @@ -50,7 +49,7 @@ public static IDictionary<string, string> GetOrCreateEntityTypeErrors(this IRead
public static void SetEntityTypeErrors(this IMutableModel model, IDictionary<string, string> value)
=> model.SetAnnotation(
ScaffoldingAnnotationNames.EntityTypeErrors,
Check.NotNull(value, nameof(value)));
value);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -70,6 +69,6 @@ public static void SetEntityTypeErrors(this IMutableModel model, IDictionary<str
public static void SetDatabaseName(this IMutableModel model, string? value)
=> model.SetAnnotation(
ScaffoldingAnnotationNames.DatabaseName,
Check.NullButNotEmpty(value, nameof(value)));
value);
}
}
Loading

0 comments on commit 0af77cc

Please sign in to comment.