Skip to content

Commit 39e3147

Browse files
committed
Set VersionHistory to init only
With mapped collections, items are added to the collection after they are mapped. With compatible properties, however, the property value is simply set as a reference to the original object. If the property is a collection, there's no evaluation or mapping of the individual items in the collection. As a result, the property must be settable—even though it's a collection, and we don't normally want collection-valued properties to be settable. Previously, that necessitated the suppression of `CA2227`. With the introduction of `init`, however, we can replace the `set` with `init` to workaround this. Once we target .NET 5, this won't work and we'll need to reintroduce the suppression. But even then, `init` better communicates the expectations for this property. And, in fact, most new view models are actually implemented as `record` types to enforce this.
1 parent 62e6d2f commit 39e3147

1 file changed

Lines changed: 1 addition & 2 deletions

File tree

OnTopic.Tests/ViewModels/CompatiblePropertyTopicViewModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ namespace OnTopic.Tests.ViewModels {
2020
/// <remarks>
2121
/// This is a sample class intended for test purposes only; it is not designed for use in a production environment.
2222
/// </remarks>
23-
[SuppressMessage("Usage", "CA2227", Justification = "This is intended to be initialized by the mapping service.")]
2423
public class CompatiblePropertyTopicViewModel {
2524

2625
public ModelType ModelType { get; set; }
2726

28-
public Collection<DateTime>? VersionHistory { get; set; }
27+
public Collection<DateTime>? VersionHistory { get; init; }
2928

3029
} //Class
3130
} //Namespace

0 commit comments

Comments
 (0)