Skip to content

Commit defbb65

Browse files
authored
Reduce record usage in published projects (#48907)
* Convert CollectionModelBinder to class * Convert AltSvcHeader to class * Convert InternalHeader to class * Convert OnRegistrationClose to struct * Convert UnacknowledgedRenderBatch to class * Convert OptionsRecord to struct * Convert BrowserOptions to class * Convert LogEntry to class * Convert BodyDescriptorInfo to class * Convert RouteParameter to class * Convert InternalAccessTokenResult to struct * Clean up spacing * Convert DfaBuilderWorkerWorkItem to struct * Fix property names * Set properties to init * Clean up * Convert ComponentTypeInfoCacheEntry to class * Convert FormDataConverterReadParameters to class * Convert ComponentIdAndDepth to struct * Convert MethodInfoData to struct * Convert BrowserTab to class * Revert unrelated changes * Convert NamedEvent to struct * Revert submodule changes * Add Deconstruct to ComponentTypeInfoCacheEntry * Seal InternalHeader * Convert FormDataConverterReadParameters to struct
1 parent 5490dbb commit defbb65

File tree

16 files changed

+317
-67
lines changed

16 files changed

+317
-67
lines changed

src/Components/Components/src/ComponentFactory.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,26 @@ void Initialize(IServiceProvider serviceProvider, IComponent component)
134134
}
135135

136136
// Tracks information about a specific component type that ComponentFactory uses
137-
private record class ComponentTypeInfoCacheEntry(
138-
IComponentRenderMode? ComponentTypeRenderMode,
139-
Action<IServiceProvider, IComponent> PerformPropertyInjection);
137+
private sealed class ComponentTypeInfoCacheEntry
138+
{
139+
public IComponentRenderMode? ComponentTypeRenderMode { get; }
140+
141+
public Action<IServiceProvider, IComponent> PerformPropertyInjection { get; }
142+
143+
public ComponentTypeInfoCacheEntry(
144+
IComponentRenderMode? componentTypeRenderMode,
145+
Action<IServiceProvider, IComponent> performPropertyInjection)
146+
{
147+
ComponentTypeRenderMode = componentTypeRenderMode;
148+
PerformPropertyInjection = performPropertyInjection;
149+
}
150+
151+
public void Deconstruct(
152+
out IComponentRenderMode? componentTypeRenderMode,
153+
out Action<IServiceProvider, IComponent> performPropertyInjection)
154+
{
155+
componentTypeRenderMode = ComponentTypeRenderMode;
156+
performPropertyInjection = PerformPropertyInjection;
157+
}
158+
}
140159
}

src/Components/Endpoints/src/FormMapping/Factories/ComplexType/ComplexTypeExpressionConverterFactoryOfT.cs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,44 @@ private static FormDataConverterReadParameters CreateFormDataConverterParameters
576576
Expression.Parameter(typeof(bool).MakeByRefType(), "foundValue"));
577577
}
578578

579-
private record struct FormDataConverterReadParameters(
580-
ParameterExpression ReaderParam,
581-
ParameterExpression TypeParam,
582-
ParameterExpression OptionsParam,
583-
ParameterExpression ResultParam,
584-
ParameterExpression FoundValueParam);
579+
private readonly struct FormDataConverterReadParameters
580+
{
581+
public ParameterExpression ReaderParam { get; }
582+
583+
public ParameterExpression TypeParam { get; }
584+
585+
public ParameterExpression OptionsParam { get; }
586+
587+
public ParameterExpression ResultParam { get; }
588+
589+
public ParameterExpression FoundValueParam { get; }
590+
591+
public FormDataConverterReadParameters(
592+
ParameterExpression readerParam,
593+
ParameterExpression typeParam,
594+
ParameterExpression optionsParam,
595+
ParameterExpression resultParam,
596+
ParameterExpression foundValueParam)
597+
{
598+
ReaderParam = readerParam;
599+
TypeParam = typeParam;
600+
OptionsParam = optionsParam;
601+
ResultParam = resultParam;
602+
FoundValueParam = foundValueParam;
603+
}
604+
605+
public void Deconstruct(
606+
out ParameterExpression readerParam,
607+
out ParameterExpression typeParam,
608+
out ParameterExpression optionsParam,
609+
out ParameterExpression resultParam,
610+
out ParameterExpression foundValueParam)
611+
{
612+
readerParam = ReaderParam;
613+
typeParam = TypeParam;
614+
optionsParam = OptionsParam;
615+
resultParam = ResultParam;
616+
foundValueParam = FoundValueParam;
617+
}
618+
}
585619
}

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.Streaming.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,29 @@ private void WriteComponentHtml(int componentId, TextWriter output, bool allowBo
243243
}
244244
}
245245

246-
private readonly record struct ComponentIdAndDepth(int ComponentId, int Depth);
247-
private readonly record struct SequenceAndKey(int Sequence, object? Key);
246+
private readonly struct ComponentIdAndDepth
247+
{
248+
public int ComponentId { get; }
249+
250+
public int Depth { get; }
251+
252+
public ComponentIdAndDepth(int componentId, int depth)
253+
{
254+
ComponentId = componentId;
255+
Depth = depth;
256+
}
257+
}
258+
259+
private readonly struct SequenceAndKey
260+
{
261+
public int Sequence { get; }
262+
263+
public object? Key { get; }
264+
265+
public SequenceAndKey(int sequence, object? key)
266+
{
267+
Sequence = sequence;
268+
Key = key;
269+
}
270+
}
248271
}

src/Components/Shared/src/ExpressionFormatting/ExpressionFormatter.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,34 +160,34 @@ static MethodInfoData GetMethodInfoData(MethodInfo methodInfo)
160160
var declaringType = methodInfo.DeclaringType;
161161
if (declaringType is null)
162162
{
163-
return new(IsSingleArgumentIndexer: false);
163+
return new(isSingleArgumentIndexer: false);
164164
}
165165

166166
// Check whether GetDefaultMembers() (if present in CoreCLR) would return a member of this type. Compiler
167167
// names the indexer property, if any, in a generated [DefaultMember] attribute for the containing type.
168168
var defaultMember = declaringType.GetCustomAttribute<DefaultMemberAttribute>(inherit: true);
169169
if (defaultMember is null)
170170
{
171-
return new(IsSingleArgumentIndexer: false);
171+
return new(isSingleArgumentIndexer: false);
172172
}
173173

174174
// Find default property (the indexer) and confirm its getter is the method in this expression.
175175
var runtimeProperties = declaringType.GetRuntimeProperties();
176176
if (runtimeProperties is null)
177177
{
178-
return new(IsSingleArgumentIndexer: false);
178+
return new(isSingleArgumentIndexer: false);
179179
}
180180

181181
foreach (var property in runtimeProperties)
182182
{
183183
if (string.Equals(defaultMember.MemberName, property.Name, StringComparison.Ordinal) &&
184184
property.GetMethod == methodInfo)
185185
{
186-
return new(IsSingleArgumentIndexer: true);
186+
return new(isSingleArgumentIndexer: true);
187187
}
188188
}
189189

190-
return new(IsSingleArgumentIndexer: false);
190+
return new(isSingleArgumentIndexer: false);
191191
}
192192
}
193193

@@ -297,5 +297,13 @@ private static void FormatConstantValue(ConstantExpression constantExpression, r
297297
}
298298
}
299299

300-
private record struct MethodInfoData(bool IsSingleArgumentIndexer);
300+
private readonly struct MethodInfoData
301+
{
302+
public bool IsSingleArgumentIndexer { get; }
303+
304+
public MethodInfoData(bool isSingleArgumentIndexer)
305+
{
306+
IsSingleArgumentIndexer = isSingleArgumentIndexer;
307+
}
308+
}
301309
}

src/Components/WebAssembly/Server/src/TargetPickerUi.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,29 @@ private async Task<IEnumerable<BrowserTab>> GetOpenedBrowserTabs()
425425
return JsonSerializer.Deserialize<BrowserTab[]>(jsonResponse, JsonOptions)!;
426426
}
427427

428-
private sealed record BrowserTab
429-
(
430-
string Id,
431-
string Type,
432-
string Url,
433-
string Title,
434-
string DevtoolsFrontendUrl,
435-
string WebSocketDebuggerUrl
436-
);
428+
private sealed class BrowserTab
429+
{
430+
public string Id { get; }
431+
public string Type { get; }
432+
public string Url { get; }
433+
public string Title { get; }
434+
public string DevtoolsFrontendUrl { get; }
435+
public string WebSocketDebuggerUrl { get; }
436+
437+
public BrowserTab(
438+
string id,
439+
string type,
440+
string url,
441+
string title,
442+
string devtoolsFrontendUrl,
443+
string webSocketDebuggerUrl)
444+
{
445+
Id = id;
446+
Type = type;
447+
Url = url;
448+
Title = title;
449+
DevtoolsFrontendUrl = devtoolsFrontendUrl;
450+
WebSocketDebuggerUrl = webSocketDebuggerUrl;
451+
}
452+
}
437453
}

src/Components/WebAssembly/WebAssembly.Authentication/src/Models/InteractiveRequestOptions.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,32 @@ public override void Write(Utf8JsonWriter writer, InteractiveRequestOptions valu
115115
InteractiveRequestOptionsSerializerContext.Default.OptionsRecord);
116116
}
117117

118-
internal record struct OptionsRecord(
119-
[property: JsonInclude] string ReturnUrl,
120-
[property: JsonInclude] IEnumerable<string> Scopes,
121-
[property: JsonInclude] InteractionType Interaction,
122-
[property: JsonInclude] Dictionary<string, object>? AdditionalRequestParameters);
118+
internal readonly struct OptionsRecord
119+
{
120+
[JsonInclude]
121+
public string ReturnUrl { get; init; }
122+
123+
[JsonInclude]
124+
public IEnumerable<string> Scopes { get; init; }
125+
126+
[JsonInclude]
127+
public InteractionType Interaction { get; init; }
128+
129+
[JsonInclude]
130+
public Dictionary<string, object>? AdditionalRequestParameters { get; init; }
131+
132+
public OptionsRecord(
133+
string returnUrl,
134+
IEnumerable<string> scopes,
135+
InteractionType interaction,
136+
Dictionary<string, object>? additionalRequestParameters)
137+
{
138+
ReturnUrl = returnUrl;
139+
Scopes = scopes;
140+
Interaction = interaction;
141+
AdditionalRequestParameters = additionalRequestParameters;
142+
}
143+
}
123144
}
124145
}
125146

src/Components/WebAssembly/WebAssembly.Authentication/src/Services/RemoteAuthenticationService.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,16 @@ internal class RemoteAuthenticationServiceJavaScriptLoggingOptions
255255
}
256256

257257
// Internal for testing purposes
258-
internal record struct InternalAccessTokenResult([property: JsonConverter(typeof(JsonStringEnumConverter<AccessTokenResultStatus>))] AccessTokenResultStatus Status, AccessToken Token);
258+
internal readonly struct InternalAccessTokenResult
259+
{
260+
[JsonConverter(typeof(JsonStringEnumConverter<AccessTokenResultStatus>))]
261+
public AccessTokenResultStatus Status { get; init; }
262+
263+
public AccessToken Token { get; init; }
264+
265+
public InternalAccessTokenResult(AccessTokenResultStatus status, AccessToken token)
266+
{
267+
Status = status;
268+
Token = token;
269+
}
270+
}

src/Components/WebView/WebView/src/Services/WebViewRenderer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ public void NotifyRenderCompleted(long batchId)
7878
nextUnacknowledgedBatch.CompletionSource.SetResult();
7979
}
8080

81-
private sealed record UnacknowledgedRenderBatch
81+
private sealed class UnacknowledgedRenderBatch
8282
{
8383
public long BatchId { get; init; }
84+
8485
public TaskCompletionSource CompletionSource { get; init; }
8586
}
8687
}

src/Grpc/JsonTranscoding/src/Shared/ServiceDescriptorHelpers.cs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,11 @@ public static Dictionary<string, RouteParameter> ResolveRouteParameterDescriptor
428428
{
429429
// A repeating field isn't a message type. The JSON parser will parse using the containing
430430
// type to get the repeating collection.
431-
return new BodyDescriptorInfo(responseBodyDescriptor.ContainingType, responseBodyDescriptor, IsDescriptorRepeated: true, propertyInfo);
431+
return new BodyDescriptorInfo(responseBodyDescriptor.ContainingType, responseBodyDescriptor, isDescriptorRepeated: true, propertyInfo);
432432
}
433433
else
434434
{
435-
return new BodyDescriptorInfo(responseBodyDescriptor.MessageType, responseBodyDescriptor, IsDescriptorRepeated: false, propertyInfo);
435+
return new BodyDescriptorInfo(responseBodyDescriptor.MessageType, responseBodyDescriptor, isDescriptorRepeated: false, propertyInfo);
436436
}
437437
}
438438
else
@@ -444,7 +444,7 @@ public static Dictionary<string, RouteParameter> ResolveRouteParameterDescriptor
444444
requestParameter = methodInfo.GetParameters().SingleOrDefault(p => p.Name == "request");
445445
}
446446

447-
return new BodyDescriptorInfo(methodDescriptor.InputType, FieldDescriptor: null, IsDescriptorRepeated: false, ParameterInfo: requestParameter);
447+
return new BodyDescriptorInfo(methodDescriptor.InputType, fieldDescriptor: null, isDescriptorRepeated: false, parameterInfo: requestParameter);
448448
}
449449
}
450450

@@ -562,12 +562,32 @@ private static bool IsCustomType(MessageDescriptor messageDescriptor)
562562
return false;
563563
}
564564

565-
public sealed record BodyDescriptorInfo(
566-
MessageDescriptor Descriptor,
567-
FieldDescriptor? FieldDescriptor,
568-
bool IsDescriptorRepeated,
569-
PropertyInfo? PropertyInfo = null,
570-
ParameterInfo? ParameterInfo = null);
565+
public sealed class BodyDescriptorInfo
566+
{
567+
public MessageDescriptor Descriptor { get; }
568+
569+
public FieldDescriptor? FieldDescriptor { get; }
570+
571+
public bool IsDescriptorRepeated { get; }
572+
573+
public PropertyInfo? PropertyInfo { get; }
574+
575+
public ParameterInfo? ParameterInfo { get; }
576+
577+
public BodyDescriptorInfo(
578+
MessageDescriptor descriptor,
579+
FieldDescriptor? fieldDescriptor,
580+
bool isDescriptorRepeated,
581+
PropertyInfo? propertyInfo = null,
582+
ParameterInfo? parameterInfo = null)
583+
{
584+
Descriptor = descriptor;
585+
FieldDescriptor = fieldDescriptor;
586+
IsDescriptorRepeated = isDescriptorRepeated;
587+
PropertyInfo = propertyInfo;
588+
ParameterInfo = parameterInfo;
589+
}
590+
}
571591

572592
public static string FormatUnderscoreName(string input, bool pascalCase, bool preservePeriod)
573593
{
@@ -626,7 +646,21 @@ public static string FormatUnderscoreName(string input, bool pascalCase, bool pr
626646
}
627647
}
628648

629-
internal record RouteParameter(
630-
List<FieldDescriptor> DescriptorsPath,
631-
HttpRouteVariable RouteVariable,
632-
string JsonPath);
649+
internal sealed class RouteParameter
650+
{
651+
public List<FieldDescriptor> DescriptorsPath { get; }
652+
653+
public HttpRouteVariable RouteVariable { get; }
654+
655+
public string JsonPath { get; }
656+
657+
public RouteParameter(
658+
List<FieldDescriptor> descriptorsPath,
659+
HttpRouteVariable routeVariable,
660+
string jsonPath)
661+
{
662+
DescriptorsPath = descriptorsPath;
663+
RouteVariable = routeVariable;
664+
JsonPath = jsonPath;
665+
}
666+
}

src/Http/Routing/src/Matching/DfaMatcherBuilder.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,5 +987,26 @@ private static bool TryGetRequiredValue(RoutePattern routePattern, RoutePatternP
987987
return !RouteValueEqualityComparer.Default.Equals(value, string.Empty);
988988
}
989989

990-
private readonly record struct DfaBuilderWorkerWorkItem(RouteEndpoint Endpoint, int PrecedenceDigit, List<DfaNode> Parents);
990+
public readonly struct DfaBuilderWorkerWorkItem
991+
{
992+
public RouteEndpoint Endpoint { get; }
993+
994+
public int PrecedenceDigit { get; }
995+
996+
public List<DfaNode> Parents { get; }
997+
998+
public DfaBuilderWorkerWorkItem(RouteEndpoint endpoint, int precedenceDigit, List<DfaNode> parents)
999+
{
1000+
Endpoint = endpoint;
1001+
PrecedenceDigit = precedenceDigit;
1002+
Parents = parents;
1003+
}
1004+
1005+
public void Deconstruct(out RouteEndpoint endpoint, out int precedenceDigit, out List<DfaNode> parents)
1006+
{
1007+
endpoint = Endpoint;
1008+
precedenceDigit = PrecedenceDigit;
1009+
parents = Parents;
1010+
}
1011+
}
9911012
}

0 commit comments

Comments
 (0)