Skip to content

Commit

Permalink
Design: Annotate NRTs
Browse files Browse the repository at this point in the history
Part of #19007
  • Loading branch information
bricelam committed Mar 12, 2021
1 parent f1a0d8c commit 050dad6
Show file tree
Hide file tree
Showing 66 changed files with 600 additions and 520 deletions.
12 changes: 6 additions & 6 deletions src/EFCore.Design/Design/DbContextActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public static class DbContextActivator
/// <returns> The newly created object. </returns>
public static DbContext CreateInstance(
[NotNull] Type contextType,
[CanBeNull] Assembly startupAssembly = null,
[CanBeNull] IOperationReportHandler reportHandler = null)
[CanBeNull] Assembly? startupAssembly = null,
[CanBeNull] IOperationReportHandler? reportHandler = null)
=> CreateInstance(contextType, startupAssembly, reportHandler, null);

/// <summary>
Expand All @@ -41,9 +41,9 @@ public static DbContext CreateInstance(
/// <returns> The newly created object. </returns>
public static DbContext CreateInstance(
[NotNull] Type contextType,
[CanBeNull] Assembly startupAssembly,
[CanBeNull] IOperationReportHandler reportHandler,
[CanBeNull] string[] args)
[CanBeNull] Assembly? startupAssembly,
[CanBeNull] IOperationReportHandler? reportHandler,
[CanBeNull] string[]? args)
{
Check.NotNull(contextType, nameof(contextType));

Expand All @@ -52,7 +52,7 @@ public static DbContext CreateInstance(
contextType.Assembly,
startupAssembly ?? contextType.Assembly,
args: args ?? Array.Empty<string>())
.CreateContext(contextType.FullName);
.CreateContext(contextType.FullName!);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public static class DesignTimeServiceCollectionExtensions
/// <returns> The <paramref name="services" />. This enables chaining additional method calls. </returns>
public static IServiceCollection AddEntityFrameworkDesignTimeServices(
[NotNull] this IServiceCollection services,
[CanBeNull] IOperationReporter reporter = null,
[CanBeNull] Func<IServiceProvider> applicationServiceProviderAccessor = null)
[CanBeNull] IOperationReporter? reporter = null,
[CanBeNull] Func<IServiceProvider>? applicationServiceProviderAccessor = null)
{
if (reporter == null)
{
Expand Down Expand Up @@ -84,7 +84,7 @@ public static IServiceCollection AddEntityFrameworkDesignTimeServices(
.AddSingleton<IReverseEngineerScaffolder, ReverseEngineerScaffolder>()
.AddSingleton<IScaffoldingModelFactory, RelationalScaffoldingModelFactory>()
.AddSingleton<IScaffoldingTypeMapper, ScaffoldingTypeMapper>()
.AddSingleton<ITypeMappingSource>(p => p.GetService<IRelationalTypeMappingSource>())
.AddSingleton<ITypeMappingSource>(p => p.GetRequiredService<IRelationalTypeMappingSource>())
.AddSingleton<IValueConverterSelector, ValueConverterSelector>()
.AddSingleton<MigrationsCodeGeneratorDependencies>()
.AddSingleton<ModelCodeGeneratorDependencies>()
Expand Down
10 changes: 5 additions & 5 deletions src/EFCore.Design/Design/ICSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,31 @@ public interface ICSharpHelper
/// <param name="name"> The base identifier name. </param>
/// <param name="scope"> A list of in-scope identifiers. </param>
/// <returns> The identifier. </returns>
string Identifier([NotNull] string name, [CanBeNull] ICollection<string> scope = null);
string Identifier([NotNull] string name, [CanBeNull] ICollection<string>? scope = null);

/// <summary>
/// Generates a property accessor lambda.
/// </summary>
/// <param name="properties"> The property names. </param>
/// <param name="lambdaIdentifier"> The identifier to use for parameter in the lambda. </param>
/// <returns> The lambda. </returns>
string Lambda([NotNull] IReadOnlyList<string> properties, [CanBeNull] string lambdaIdentifier = null);
string Lambda([NotNull] IReadOnlyList<string> properties, [CanBeNull] string? lambdaIdentifier = null);

/// <summary>
/// Generates a property accessor lambda.
/// </summary>
/// <param name="properties"> The properties. </param>
/// <param name="lambdaIdentifier"> The identifier to use for parameter in the lambda. </param>
/// <returns> The lambda. </returns>
string Lambda([NotNull] IEnumerable<IProperty> properties, [CanBeNull] string lambdaIdentifier = null)
string Lambda([NotNull] IEnumerable<IProperty> properties, [CanBeNull] string? lambdaIdentifier = null)
=> Lambda(properties.Select(p => p.Name).ToList(), lambdaIdentifier);

/// <summary>
/// Generates a multidimensional array literal.
/// </summary>
/// <param name="values"> The multidimensional array. </param>
/// <returns> The literal. </returns>
string Literal([NotNull] object[,] values);
string Literal([NotNull] object?[,] values);

/// <summary>
/// Generates a nullable literal.
Expand Down Expand Up @@ -222,6 +222,6 @@ string Literal<T>(T? value)
/// </summary>
/// <param name="value"> The value. </param>
/// <returns> The literal. </returns>
string UnknownLiteral([CanBeNull] object value);
string UnknownLiteral([CanBeNull] object? value);
}
}
2 changes: 1 addition & 1 deletion src/EFCore.Design/Design/ILanguageBasedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public interface ILanguageBasedService
/// Gets the programming language supported by this service.
/// </summary>
/// <value> The language. </value>
string Language { get; }
string? Language { get; }
}
}
2 changes: 0 additions & 2 deletions src/EFCore.Design/Design/IOperationResultHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using JetBrains.Annotations;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Design
{
/// <summary>
Expand Down
7 changes: 5 additions & 2 deletions src/EFCore.Design/Design/IPluralizer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;

namespace Microsoft.EntityFrameworkCore.Design
Expand All @@ -16,14 +17,16 @@ public interface IPluralizer
/// </summary>
/// <param name="identifier"> The identifier to be pluralized. </param>
/// <returns> The pluralized identifier. </returns>
string Pluralize([CanBeNull] string identifier);
[return: NotNullIfNotNull("identifier")]
string? Pluralize([CanBeNull] string? identifier);

/// <summary>
/// Gets the singular version of the given identifier. Returns the same
/// identifier if it is already singularized.
/// </summary>
/// <param name="identifier"> The identifier to be singularized. </param>
/// <returns> The singularized identifier. </returns>
string Singularize([CanBeNull] string identifier);
[return: NotNullIfNotNull("identifier")]
string? Singularize([CanBeNull] string? identifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public virtual IServiceProvider Create([NotNull] string[] args)
?? CreateEmptyServiceProvider();
}

private IServiceProvider CreateFromHosting(string[] args)
private IServiceProvider? CreateFromHosting(string[] args)
{
_reporter.WriteVerbose(DesignStrings.FindingHostingServices);

Expand Down Expand Up @@ -94,7 +94,7 @@ private IServiceProvider CreateFromHosting(string[] args)
{
if (ex is TargetInvocationException)
{
ex = ex.InnerException;
ex = ex.InnerException!;
}

_reporter.WriteVerbose(ex.ToString());
Expand Down
30 changes: 15 additions & 15 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public CSharpHelper([NotNull] IRelationalTypeMappingSource relationalTypeMapping
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string Lambda(IReadOnlyList<string> properties, string lambdaIdentifier)
public virtual string Lambda(IReadOnlyList<string> properties, string? lambdaIdentifier)
{
Check.NotNull(properties, nameof(properties));
Check.NullButNotEmpty(lambdaIdentifier, nameof(lambdaIdentifier));
Expand Down Expand Up @@ -232,7 +232,7 @@ private string Reference(Type type, bool useFullName)
if (type.IsArray)
{
builder
.Append(Reference(type.GetElementType()))
.Append(Reference(type.GetElementType()!))
.Append("[");

var rank = type.GetArrayRank();
Expand Down Expand Up @@ -268,7 +268,7 @@ private string Reference(Type type, bool useFullName)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string Identifier(string name, ICollection<string> scope = null)
public virtual string Identifier(string name, ICollection<string>? scope = null)
{
Check.NotEmpty(name, nameof(name));

Expand Down Expand Up @@ -592,7 +592,7 @@ private string Array(Type type, IEnumerable values, bool vertical = false)

builder.Append("new");

var valuesList = values.Cast<object>().ToList();
var valuesList = values.Cast<object?>().ToList();

if (valuesList.Count == 0)
{
Expand Down Expand Up @@ -657,7 +657,7 @@ private string Array(Type type, IEnumerable values, bool vertical = false)

builder.Append(
byteArray
? Literal((int)(byte)value)
? Literal((int)(byte)value!)
: UnknownLiteral(value));
}

Expand All @@ -683,7 +683,7 @@ private string Array(Type type, IEnumerable values, bool vertical = false)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string Literal(object[,] values)
public virtual string Literal(object?[,] values)
{
var builder = new IndentedStringBuilder();

Expand Down Expand Up @@ -769,11 +769,11 @@ protected virtual string GetCompositeEnumValue([NotNull] Type type, [NotNull] En
}

return allValues.Aggregate(
(string)null,
(string?)null,
(previous, current) =>
previous == null
? GetSimpleEnumValue(type, Enum.GetName(type, current))
: previous + " | " + GetSimpleEnumValue(type, Enum.GetName(type, current)));
? GetSimpleEnumValue(type, Enum.GetName(type, current)!)
: previous + " | " + GetSimpleEnumValue(type, Enum.GetName(type, current)!))!;
}

internal static IReadOnlyCollection<Enum> GetFlags(Enum flags)
Expand Down Expand Up @@ -803,7 +803,7 @@ internal static IReadOnlyCollection<Enum> GetFlags(Enum flags)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string UnknownLiteral(object value)
public virtual string UnknownLiteral(object? value)
{
if (value == null)
{
Expand All @@ -824,7 +824,7 @@ public virtual string UnknownLiteral(object value)

if (value is Array array)
{
return Array(literalType.GetElementType(), array);
return Array(literalType.GetElementType()!, array);
}

var mapping = _relationalTypeMappingSource.FindMapping(literalType);
Expand Down Expand Up @@ -856,7 +856,7 @@ private bool HandleExpression(Expression expression, StringBuilder builder, bool
case ExpressionType.NewArrayInit:
builder
.Append("new ")
.Append(Reference(expression.Type.GetElementType()))
.Append(Reference(expression.Type.GetElementType()!))
.Append("[] { ");

HandleList(((NewArrayExpression)expression).Expressions, builder, simple: true);
Expand Down Expand Up @@ -884,11 +884,11 @@ private bool HandleExpression(Expression expression, StringBuilder builder, bool
if (callExpression.Method.IsStatic)
{
builder
.Append(Reference(callExpression.Method.DeclaringType, useFullName: true));
.Append(Reference(callExpression.Method.DeclaringType!, useFullName: true));
}
else
{
if (!HandleExpression(callExpression.Object, builder))
if (!HandleExpression(callExpression.Object!, builder))
{
return false;
}
Expand Down Expand Up @@ -918,7 +918,7 @@ private bool HandleExpression(Expression expression, StringBuilder builder, bool
if (memberExpression.Expression == null)
{
builder
.Append(Reference(memberExpression.Member.DeclaringType, useFullName: true));
.Append(Reference(memberExpression.Member.DeclaringType!, useFullName: true));
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions src/EFCore.Design/Design/Internal/ContextInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,30 @@ public class ContextInfo
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string ProviderName { get; [param: NotNull] set; }
public virtual string ProviderName { get; [param: NotNull] set; } = null!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string DatabaseName { get; [param: NotNull] set; }
public virtual string DatabaseName { get; [param: NotNull] set; } = null!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string DataSource { get; [param: NotNull] set; }
public virtual string DataSource { get; [param: NotNull] set; } = null!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string Options { get; [param: NotNull] set; }
public virtual string Options { get; [param: NotNull] set; } = null!;
}
}
31 changes: 15 additions & 16 deletions src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class DatabaseOperations
{
private readonly IOperationReporter _reporter;
private readonly string _projectDir;
private readonly string _rootNamespace;
private readonly string _language;
private readonly string? _rootNamespace;
private readonly string? _language;
private readonly DesignTimeServicesBuilder _servicesBuilder;
private readonly string[] _args;

Expand All @@ -38,16 +38,13 @@ public DatabaseOperations(
[NotNull] Assembly assembly,
[NotNull] Assembly startupAssembly,
[NotNull] string projectDir,
[NotNull] string rootNamespace,
[CanBeNull] string language,
[NotNull] string[] args)
[CanBeNull] string? rootNamespace,
[CanBeNull] string? language,
[CanBeNull] string[]? args)
{
Check.NotNull(reporter, nameof(reporter));
Check.NotNull(startupAssembly, nameof(startupAssembly));
Check.NotNull(projectDir, nameof(projectDir));
Check.NotNull(rootNamespace, nameof(rootNamespace));
// Note: cannot assert that args is not null - as old versions of
// tools can still pass null.

_reporter = reporter;
_projectDir = projectDir;
Expand All @@ -67,13 +64,13 @@ public DatabaseOperations(
public virtual SavedModelFiles ScaffoldContext(
[NotNull] string provider,
[NotNull] string connectionString,
[CanBeNull] string outputDir,
[CanBeNull] string outputContextDir,
[CanBeNull] string dbContextClassName,
[CanBeNull] string? outputDir,
[CanBeNull] string? outputContextDir,
[CanBeNull] string? dbContextClassName,
[NotNull] IEnumerable<string> schemas,
[NotNull] IEnumerable<string> tables,
[CanBeNull] string modelNamespace,
[CanBeNull] string contextNamespace,
[CanBeNull] string? modelNamespace,
[CanBeNull] string? contextNamespace,
bool useDataAnnotations,
bool overwriteFiles,
bool useDatabaseNames,
Expand Down Expand Up @@ -123,18 +120,20 @@ public virtual SavedModelFiles ScaffoldContext(
overwriteFiles);
}

private string GetNamespaceFromOutputPath(string directoryPath)
private string? GetNamespaceFromOutputPath(string directoryPath)
{
var subNamespace = SubnamespaceFromOutputPath(_projectDir, directoryPath);
return string.IsNullOrEmpty(subNamespace)
? _rootNamespace
: _rootNamespace + "." + subNamespace;
: string.IsNullOrEmpty(_rootNamespace)
? subNamespace
: _rootNamespace + "." + subNamespace;
}

// if outputDir is a subfolder of projectDir, then use each subfolder as a subnamespace
// --output-dir $(projectFolder)/A/B/C
// => "namespace $(rootnamespace).A.B.C"
private static string SubnamespaceFromOutputPath(string projectDir, string outputDir)
private static string? SubnamespaceFromOutputPath(string projectDir, string outputDir)
{
if (!outputDir.StartsWith(projectDir, StringComparison.Ordinal))
{
Expand Down
Loading

0 comments on commit 050dad6

Please sign in to comment.