Skip to content

Commit 7c5c1fc

Browse files
authored
First nullability work on metadata (dotnet#23133)
* First nullability work on metadata Annotatable, EntityType and related types * Address review comments
1 parent 25727cd commit 7c5c1fc

12 files changed

+375
-345
lines changed

src/EFCore/Infrastructure/Annotatable.cs

+19-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using Microsoft.EntityFrameworkCore.Metadata;
1111
using Microsoft.EntityFrameworkCore.Utilities;
1212

13+
#nullable enable
14+
1315
namespace Microsoft.EntityFrameworkCore.Infrastructure
1416
{
1517
/// <summary>
@@ -23,7 +25,7 @@ namespace Microsoft.EntityFrameworkCore.Infrastructure
2325
/// </summary>
2426
public class Annotatable : IMutableAnnotatable
2527
{
26-
private SortedDictionary<string, Annotation> _annotations;
28+
private SortedDictionary<string, Annotation>? _annotations;
2729

2830
/// <summary>
2931
/// Gets all annotations on the current object.
@@ -37,7 +39,7 @@ public virtual IEnumerable<Annotation> GetAnnotations()
3739
/// <param name="name"> The key of the annotation to be added. </param>
3840
/// <param name="value"> The value to be stored in the annotation. </param>
3941
/// <returns> The newly added annotation. </returns>
40-
public virtual Annotation AddAnnotation([NotNull] string name, [CanBeNull] object value)
42+
public virtual Annotation AddAnnotation([NotNull] string name, [CanBeNull] object? value)
4143
{
4244
Check.NotEmpty(name, nameof(name));
4345

@@ -70,7 +72,7 @@ protected virtual Annotation AddAnnotation([NotNull] string name, [NotNull] Anno
7072
/// </summary>
7173
/// <param name="name"> The key of the annotation to be added. </param>
7274
/// <param name="value"> The value to be stored in the annotation. </param>
73-
public virtual void SetAnnotation(string name, object value)
75+
public virtual void SetAnnotation(string name, object? value)
7476
{
7577
var oldAnnotation = FindAnnotation(name);
7678
if (oldAnnotation != null
@@ -90,10 +92,10 @@ public virtual void SetAnnotation(string name, object value)
9092
/// <param name="annotation"> The annotation to be set. </param>
9193
/// <param name="oldAnnotation"> The annotation being replaced. </param>
9294
/// <returns> The annotation that was set. </returns>
93-
protected virtual Annotation SetAnnotation(
95+
protected virtual Annotation? SetAnnotation(
9496
[NotNull] string name,
9597
[NotNull] Annotation annotation,
96-
[CanBeNull] Annotation oldAnnotation)
98+
[CanBeNull] Annotation? oldAnnotation)
9799
{
98100
if (_annotations == null)
99101
{
@@ -112,10 +114,10 @@ protected virtual Annotation SetAnnotation(
112114
/// <param name="annotation"> The annotation set. </param>
113115
/// <param name="oldAnnotation"> The old annotation. </param>
114116
/// <returns> The annotation that was set. </returns>
115-
protected virtual Annotation OnAnnotationSet(
117+
protected virtual Annotation? OnAnnotationSet(
116118
[NotNull] string name,
117-
[CanBeNull] Annotation annotation,
118-
[CanBeNull] Annotation oldAnnotation)
119+
[CanBeNull] Annotation? annotation,
120+
[CanBeNull] Annotation? oldAnnotation)
119121
=> annotation;
120122

121123
/// <summary>
@@ -125,7 +127,7 @@ protected virtual Annotation OnAnnotationSet(
125127
/// <returns>
126128
/// The existing annotation if an annotation with the specified name already exists. Otherwise, <see langword="null" />.
127129
/// </returns>
128-
public virtual Annotation FindAnnotation([NotNull] string name)
130+
public virtual Annotation? FindAnnotation([NotNull] string name)
129131
{
130132
Check.NotEmpty(name, nameof(name));
131133

@@ -141,7 +143,7 @@ public virtual Annotation FindAnnotation([NotNull] string name)
141143
/// </summary>
142144
/// <param name="name"> The annotation to remove. </param>
143145
/// <returns> The annotation that was removed. </returns>
144-
public virtual Annotation RemoveAnnotation([NotNull] string name)
146+
public virtual Annotation? RemoveAnnotation([NotNull] string name)
145147
{
146148
Check.NotNull(name, nameof(name));
147149

@@ -151,7 +153,8 @@ public virtual Annotation RemoveAnnotation([NotNull] string name)
151153
return null;
152154
}
153155

154-
_annotations.Remove(name);
156+
// TODO-NULLABLE: Put MemberNotNull on FindAnnotation when we target net5.0
157+
_annotations!.Remove(name);
155158

156159
if (_annotations.Count == 0)
157160
{
@@ -171,7 +174,7 @@ public virtual Annotation RemoveAnnotation([NotNull] string name)
171174
/// The value of the existing annotation if an annotation with the specified name already exists.
172175
/// Otherwise, <see langword="null" />.
173176
/// </returns>
174-
public virtual object this[string name]
177+
public virtual object? this[string name]
175178
{
176179
get => FindAnnotation(name)?.Value;
177180
set
@@ -195,7 +198,7 @@ public virtual object this[string name]
195198
/// <param name="name"> The key of the annotation. </param>
196199
/// <param name="value"> The value to be stored in the annotation. </param>
197200
/// <returns> The newly created annotation. </returns>
198-
protected virtual Annotation CreateAnnotation([NotNull] string name, [CanBeNull] object value)
201+
protected virtual Annotation CreateAnnotation([NotNull] string name, [CanBeNull] object? value)
199202
=> new Annotation(name, value);
200203

201204
/// <inheritdoc />
@@ -205,17 +208,17 @@ IEnumerable<IAnnotation> IAnnotatable.GetAnnotations()
205208

206209
/// <inheritdoc />
207210
[DebuggerStepThrough]
208-
IAnnotation IAnnotatable.FindAnnotation(string name)
211+
IAnnotation? IAnnotatable.FindAnnotation(string name)
209212
=> FindAnnotation(name);
210213

211214
/// <inheritdoc />
212215
[DebuggerStepThrough]
213-
IAnnotation IMutableAnnotatable.AddAnnotation(string name, object value)
216+
IAnnotation IMutableAnnotatable.AddAnnotation(string name, object? value)
214217
=> AddAnnotation(name, value);
215218

216219
/// <inheritdoc />
217220
[DebuggerStepThrough]
218-
IAnnotation IMutableAnnotatable.RemoveAnnotation(string name)
221+
IAnnotation? IMutableAnnotatable.RemoveAnnotation(string name)
219222
=> RemoveAnnotation(name);
220223
}
221224
}

src/EFCore/Infrastructure/IAnnotatable.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Collections.Generic;
55
using JetBrains.Annotations;
66

7+
#nullable enable
8+
79
namespace Microsoft.EntityFrameworkCore.Infrastructure
810
{
911
/// <summary>
@@ -24,7 +26,7 @@ public interface IAnnotatable
2426
/// <returns>
2527
/// The value of the existing annotation if an annotation with the specified name already exists. Otherwise, <see langword="null" />.
2628
/// </returns>
27-
object this[[NotNull] string name] { get; }
29+
object? this[[NotNull] string name] { get; }
2830

2931
/// <summary>
3032
/// Gets the annotation with the given name, returning <see langword="null" /> if it does not exist.
@@ -33,7 +35,7 @@ public interface IAnnotatable
3335
/// <returns>
3436
/// The existing annotation if an annotation with the specified name already exists. Otherwise, <see langword="null" />.
3537
/// </returns>
36-
IAnnotation FindAnnotation([NotNull] string name);
38+
IAnnotation? FindAnnotation([NotNull] string name);
3739

3840
/// <summary>
3941
/// Gets all annotations on the current object.

src/EFCore/Metadata/IConventionAnnotatable.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Microsoft.EntityFrameworkCore.Infrastructure;
77
using Microsoft.EntityFrameworkCore.Metadata.Builders;
88

9+
#nullable enable
10+
911
namespace Microsoft.EntityFrameworkCore.Metadata
1012
{
1113
/// <summary>
@@ -23,7 +25,7 @@ public interface IConventionAnnotatable : IAnnotatable
2325
/// <summary>
2426
/// Gets the builder that can be used to configure this object.
2527
/// </summary>
26-
IConventionAnnotatableBuilder Builder { get; }
28+
IConventionAnnotatableBuilder? Builder { get; }
2729

2830
/// <summary>
2931
/// Gets all annotations on the current object.
@@ -37,7 +39,7 @@ public interface IConventionAnnotatable : IAnnotatable
3739
/// <param name="value"> The value to be stored in the annotation. </param>
3840
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
3941
/// <returns> The newly added annotation. </returns>
40-
IConventionAnnotation AddAnnotation([NotNull] string name, [CanBeNull] object value, bool fromDataAnnotation = false);
42+
IConventionAnnotation AddAnnotation([NotNull] string name, [CanBeNull] object? value, bool fromDataAnnotation = false);
4143

4244
/// <summary>
4345
/// Sets the annotation stored under the given name. Overwrites the existing annotation if an
@@ -47,7 +49,7 @@ public interface IConventionAnnotatable : IAnnotatable
4749
/// <param name="value"> The value to be stored in the annotation. </param>
4850
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
4951
/// <returns> The new annotation. </returns>
50-
IConventionAnnotation SetAnnotation([NotNull] string name, [CanBeNull] object value, bool fromDataAnnotation = false);
52+
IConventionAnnotation SetAnnotation([NotNull] string name, [CanBeNull] object? value, bool fromDataAnnotation = false);
5153

5254
/// <summary>
5355
/// Gets the annotation with the given name, returning <see langword="null" /> if it does not exist.
@@ -56,13 +58,13 @@ public interface IConventionAnnotatable : IAnnotatable
5658
/// <returns>
5759
/// The existing annotation if an annotation with the specified name already exists. Otherwise, <see langword="null" />.
5860
/// </returns>
59-
new IConventionAnnotation FindAnnotation([NotNull] string name);
61+
new IConventionAnnotation? FindAnnotation([NotNull] string name);
6062

6163
/// <summary>
6264
/// Removes the annotation with the given name from this object.
6365
/// </summary>
6466
/// <param name="name"> The name of the annotation to remove. </param>
6567
/// <returns> The annotation that was removed. </returns>
66-
IConventionAnnotation RemoveAnnotation([NotNull] string name);
68+
IConventionAnnotation? RemoveAnnotation([NotNull] string name);
6769
}
6870
}

0 commit comments

Comments
 (0)