Skip to content

Commit

Permalink
Evolve null-checking approach in Relational (#26537)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers committed Nov 5, 2021
1 parent 1a161c0 commit 252ece7
Show file tree
Hide file tree
Showing 214 changed files with 225 additions and 2,383 deletions.
115 changes: 16 additions & 99 deletions src/EFCore.Relational/Design/AnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

#pragma warning disable EF1001 // Accessing annotation names (internal)

Expand Down Expand Up @@ -123,8 +122,6 @@ private static readonly MethodInfo _indexHasFilterNameMethodInfo
/// <param name="dependencies">Parameter object containing dependencies for this service.</param>
public AnnotationCodeGenerator(AnnotationCodeGeneratorDependencies dependencies)
{
Check.NotNull(dependencies, nameof(dependencies));

Dependencies = dependencies;
}

Expand Down Expand Up @@ -393,12 +390,7 @@ public virtual IReadOnlyList<AttributeCodeFragment> GenerateDataAnnotationAttrib
/// <see langword="false" /> if code must be generated.
/// </returns>
protected virtual bool IsHandledByConvention(IModel model, IAnnotation annotation)
{
Check.NotNull(model, nameof(model));
Check.NotNull(annotation, nameof(annotation));

return false;
}
=> false;

/// <summary>
/// Checks if the given <paramref name="annotation" /> is handled by convention when
Expand All @@ -411,12 +403,7 @@ protected virtual bool IsHandledByConvention(IModel model, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="false" />.</returns>
protected virtual bool IsHandledByConvention(IEntityType entityType, IAnnotation annotation)
{
Check.NotNull(entityType, nameof(entityType));
Check.NotNull(annotation, nameof(annotation));

return false;
}
=> false;

/// <summary>
/// Checks if the given <paramref name="annotation" /> is handled by convention when
Expand All @@ -429,12 +416,7 @@ protected virtual bool IsHandledByConvention(IEntityType entityType, IAnnotation
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="false" />.</returns>
protected virtual bool IsHandledByConvention(IKey key, IAnnotation annotation)
{
Check.NotNull(key, nameof(key));
Check.NotNull(annotation, nameof(annotation));

return false;
}
=> false;

/// <summary>
/// Checks if the given <paramref name="annotation" /> is handled by convention when
Expand All @@ -447,12 +429,7 @@ protected virtual bool IsHandledByConvention(IKey key, IAnnotation annotation)
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="false" />.</returns>
protected virtual bool IsHandledByConvention(IProperty property, IAnnotation annotation)
{
Check.NotNull(property, nameof(property));
Check.NotNull(annotation, nameof(annotation));

return false;
}
=> false;

/// <summary>
/// Checks if the given <paramref name="annotation" /> is handled by convention when
Expand All @@ -465,12 +442,7 @@ protected virtual bool IsHandledByConvention(IProperty property, IAnnotation ann
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="false" />.</returns>
protected virtual bool IsHandledByConvention(IForeignKey foreignKey, IAnnotation annotation)
{
Check.NotNull(foreignKey, nameof(foreignKey));
Check.NotNull(annotation, nameof(annotation));

return false;
}
=> false;

/// <summary>
/// Checks if the given <paramref name="annotation" /> is handled by convention when
Expand All @@ -483,12 +455,7 @@ protected virtual bool IsHandledByConvention(IForeignKey foreignKey, IAnnotation
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="false" />.</returns>
protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotation)
{
Check.NotNull(index, nameof(index));
Check.NotNull(annotation, nameof(annotation));

return false;
}
=> false;

/// <summary>
/// Returns a fluent API call for the given <paramref name="annotation" />, or <see langword="null" />
Expand All @@ -501,12 +468,7 @@ protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="null" />.</returns>
protected virtual MethodCallCodeFragment? GenerateFluentApi(IModel model, IAnnotation annotation)
{
Check.NotNull(model, nameof(model));
Check.NotNull(annotation, nameof(annotation));

return null;
}
=> null;

/// <summary>
/// Returns a fluent API call for the given <paramref name="annotation" />, or <see langword="null" />
Expand All @@ -519,12 +481,7 @@ protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="null" />.</returns>
protected virtual MethodCallCodeFragment? GenerateFluentApi(IEntityType entityType, IAnnotation annotation)
{
Check.NotNull(entityType, nameof(entityType));
Check.NotNull(annotation, nameof(annotation));

return null;
}
=> null;

/// <summary>
/// Returns a fluent API call for the given <paramref name="annotation" />, or <see langword="null" />
Expand All @@ -537,12 +494,7 @@ protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="null" />.</returns>
protected virtual MethodCallCodeFragment? GenerateFluentApi(IKey key, IAnnotation annotation)
{
Check.NotNull(key, nameof(key));
Check.NotNull(annotation, nameof(annotation));

return null;
}
=> null;

/// <summary>
/// Returns a fluent API call for the given <paramref name="annotation" />, or <see langword="null" />
Expand All @@ -555,12 +507,7 @@ protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="null" />.</returns>
protected virtual MethodCallCodeFragment? GenerateFluentApi(IProperty property, IAnnotation annotation)
{
Check.NotNull(property, nameof(property));
Check.NotNull(annotation, nameof(annotation));

return null;
}
=> null;

/// <summary>
/// Returns a fluent API call for the given <paramref name="annotation" />, or <see langword="null" />
Expand All @@ -573,12 +520,7 @@ protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="null" />.</returns>
protected virtual MethodCallCodeFragment? GenerateFluentApi(IForeignKey foreignKey, IAnnotation annotation)
{
Check.NotNull(foreignKey, nameof(foreignKey));
Check.NotNull(annotation, nameof(annotation));

return null;
}
=> null;

/// <summary>
/// Returns a fluent API call for the given <paramref name="annotation" />, or <see langword="null" />
Expand All @@ -591,12 +533,7 @@ protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="null" />.</returns>
protected virtual MethodCallCodeFragment? GenerateFluentApi(INavigation navigation, IAnnotation annotation)
{
Check.NotNull(navigation, nameof(navigation));
Check.NotNull(annotation, nameof(annotation));

return null;
}
=> null;

/// <summary>
/// Returns a fluent API call for the given <paramref name="annotation" />, or <see langword="null" />
Expand All @@ -609,12 +546,7 @@ protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="null" />.</returns>
protected virtual MethodCallCodeFragment? GenerateFluentApi(ISkipNavigation navigation, IAnnotation annotation)
{
Check.NotNull(navigation, nameof(navigation));
Check.NotNull(annotation, nameof(annotation));

return null;
}
=> null;

/// <summary>
/// Returns a fluent API call for the given <paramref name="annotation" />, or <see langword="null" />
Expand All @@ -627,12 +559,7 @@ protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="null" />.</returns>
protected virtual MethodCallCodeFragment? GenerateFluentApi(IIndex index, IAnnotation annotation)
{
Check.NotNull(index, nameof(index));
Check.NotNull(annotation, nameof(annotation));

return null;
}
=> null;

/// <summary>
/// Returns a data annotation attribute code fragment for the given <paramref name="annotation" />,
Expand All @@ -645,12 +572,7 @@ protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="null" />.</returns>
protected virtual AttributeCodeFragment? GenerateDataAnnotation(IEntityType entityType, IAnnotation annotation)
{
Check.NotNull(entityType, nameof(entityType));
Check.NotNull(annotation, nameof(annotation));

return null;
}
=> null;

/// <summary>
/// Returns a data annotation attribute code fragment for the given <paramref name="annotation" />,
Expand All @@ -663,12 +585,7 @@ protected virtual bool IsHandledByConvention(IIndex index, IAnnotation annotatio
/// <param name="annotation">The <see cref="IAnnotation" />.</param>
/// <returns><see langword="null" />.</returns>
protected virtual AttributeCodeFragment? GenerateDataAnnotation(IProperty property, IAnnotation annotation)
{
Check.NotNull(property, nameof(property));
Check.NotNull(annotation, nameof(annotation));

return null;
}
=> null;

private IEnumerable<TCodeFragment> GenerateFluentApiCallsHelper<TAnnotatable, TCodeFragment>(
TAnnotatable annotatable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Design
{
Expand Down Expand Up @@ -44,8 +43,6 @@ public sealed record AnnotationCodeGeneratorDependencies
public AnnotationCodeGeneratorDependencies(
IRelationalTypeMappingSource relationalTypeMappingSource)
{
Check.NotNull(relationalTypeMappingSource, nameof(relationalTypeMappingSource));

RelationalTypeMappingSource = relationalTypeMappingSource;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public RelationalCSharpRuntimeAnnotationCodeGenerator(
RelationalCSharpRuntimeAnnotationCodeGeneratorDependencies relationalDependencies)
: base(dependencies)
{
Check.NotNull(relationalDependencies, nameof(relationalDependencies));

RelationalDependencies = relationalDependencies;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static class RelationalDatabaseFacadeExtensions
/// </remarks>
/// <param name="databaseFacade">The <see cref="DatabaseFacade" /> for the context.</param>
public static void Migrate(this DatabaseFacade databaseFacade)
=> Check.NotNull(databaseFacade, nameof(databaseFacade)).GetRelationalService<IMigrator>().Migrate();
=> databaseFacade.GetRelationalService<IMigrator>().Migrate();

/// <summary>
/// Gets all the migrations that are defined in the configured migrations assembly.
Expand All @@ -50,7 +50,7 @@ public static void Migrate(this DatabaseFacade databaseFacade)
/// <param name="databaseFacade">The <see cref="DatabaseFacade" /> for the context.</param>
/// <returns>The list of migrations.</returns>
public static IEnumerable<string> GetMigrations(this DatabaseFacade databaseFacade)
=> Check.NotNull(databaseFacade, nameof(databaseFacade)).GetRelationalService<IMigrationsAssembly>().Migrations.Keys;
=> databaseFacade.GetRelationalService<IMigrationsAssembly>().Migrations.Keys;

/// <summary>
/// Gets all migrations that have been applied to the target database.
Expand All @@ -61,7 +61,7 @@ public static IEnumerable<string> GetMigrations(this DatabaseFacade databaseFaca
/// <param name="databaseFacade">The <see cref="DatabaseFacade" /> for the context.</param>
/// <returns>The list of migrations.</returns>
public static IEnumerable<string> GetAppliedMigrations(this DatabaseFacade databaseFacade)
=> Check.NotNull(databaseFacade, nameof(databaseFacade)).GetRelationalService<IHistoryRepository>()
=> databaseFacade.GetRelationalService<IHistoryRepository>()
.GetAppliedMigrations().Select(hr => hr.MigrationId);

/// <summary>
Expand All @@ -77,7 +77,7 @@ public static IEnumerable<string> GetAppliedMigrations(this DatabaseFacade datab
public static async Task<IEnumerable<string>> GetAppliedMigrationsAsync(
this DatabaseFacade databaseFacade,
CancellationToken cancellationToken = default)
=> (await Check.NotNull(databaseFacade, nameof(databaseFacade)).GetRelationalService<IHistoryRepository>()
=> (await databaseFacade.GetRelationalService<IHistoryRepository>()
.GetAppliedMigrationsAsync(cancellationToken).ConfigureAwait(false)).Select(hr => hr.MigrationId);

/// <summary>
Expand Down Expand Up @@ -128,7 +128,7 @@ public static async Task<IEnumerable<string>> GetPendingMigrationsAsync(
public static Task MigrateAsync(
this DatabaseFacade databaseFacade,
CancellationToken cancellationToken = default)
=> Check.NotNull(databaseFacade, nameof(databaseFacade)).GetRelationalService<IMigrator>()
=> databaseFacade.GetRelationalService<IMigrator>()
.MigrateAsync(cancellationToken: cancellationToken);

/// <summary>
Expand Down Expand Up @@ -252,7 +252,6 @@ public static int ExecuteSqlRaw(
string sql,
IEnumerable<object> parameters)
{
Check.NotNull(databaseFacade, nameof(databaseFacade));
Check.NotNull(sql, nameof(sql));
Check.NotNull(parameters, nameof(parameters));

Expand Down Expand Up @@ -458,7 +457,6 @@ public static async Task<int> ExecuteSqlRawAsync(
IEnumerable<object> parameters,
CancellationToken cancellationToken = default)
{
Check.NotNull(databaseFacade, nameof(databaseFacade));
Check.NotNull(sql, nameof(sql));
Check.NotNull(parameters, nameof(parameters));

Expand Down Expand Up @@ -828,7 +826,7 @@ public static string GenerateCreateScript(this DatabaseFacade databaseFacade)
/// <see langword="false" /> otherwise.
/// </returns>
public static bool IsRelational(this DatabaseFacade databaseFacade)
=> ((IDatabaseFacadeDependenciesAccessor)Check.NotNull(databaseFacade, nameof(databaseFacade)))
=> ((IDatabaseFacadeDependenciesAccessor)databaseFacade)
.Context.GetService<IDbContextOptions>().Extensions.OfType<RelationalOptionsExtension>().Any();

private static IRelationalDatabaseFacadeDependencies GetFacadeDependencies(DatabaseFacade databaseFacade)
Expand All @@ -845,8 +843,6 @@ private static IRelationalDatabaseFacadeDependencies GetFacadeDependencies(Datab

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

var service = databaseFacade.Instance.GetService<TService>();
if (service == null)
{
Expand All @@ -857,6 +853,6 @@ private static TService GetRelationalService<TService>(this IInfrastructure<ISer
}

private static IDbContextTransactionManager GetTransactionManager(this DatabaseFacade databaseFacade)
=> ((IDatabaseFacadeDependenciesAccessor)Check.NotNull(databaseFacade, nameof(databaseFacade))).Dependencies.TransactionManager;
=> ((IDatabaseFacadeDependenciesAccessor)databaseFacade).Dependencies.TransactionManager;
}
}
Loading

0 comments on commit 252ece7

Please sign in to comment.