1

I am not 100% sure but I have a class that looks like a good option to convert to a struct. However, the bullet point below on good reasons to I am not sure. It says single value, but I have three properties so I am not sure if that is what it is talking about. Is the class a good option to convert to a struct? It is not passed by reference and serialized via JSON.NET to the client as JSON.

It logically represents a single value, similar to primitive types (int, double, etc.).

Class

public class ScheduleVenueTravelTimeModel
{
        [JsonIgnore]
        public int VenueLocationId { get; set; }
        public int Time { get; set; }
        public int VenueId { get; set; }
}
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • 2
    a "value" can be composite, however; I wouldn't be too worried about *that*; I'd be more concerned with how it works with your serializer, and the fact that it is mutable (most `struct` types should now be `readonly struct`) – Marc Gravell May 26 '21 at 13:50
  • I wouldn't convert it based on the fact that the `JsonIgnore` attribute goes against the premise of the bullet point. If it represents a single value, then all three fields must be used and that attribute is ignoring `VenueLocationId` on serialization. – JuanR May 26 '21 at 13:54
  • Why does `JsonIgnore` go against it? This just doesnt send the property to the client for size reasons. – Mike Flynn May 26 '21 at 13:55
  • @Mike If this will be treated as a single value, then it follows that the serialized version should reflect this. Omitting `VenueLocationId` means that deserializing it will result in a different value. – JuanR May 26 '21 at 14:00
  • Interesting ok, yea that is what I didnt understand – Mike Flynn May 26 '21 at 14:00
  • 2
    I would use a separate DTO for serialisation - it looks like you're trying to use the same data type for serialisation and business logic. In this case (if your target build supports it) I'd use `public record ScheduleVenueTravelTimeDto(int Time, int VenueId);` – Matthew Watson May 26 '21 at 14:02

1 Answers1

1

It says single value, but I have three properties so I am not sure if that is what it is talking about

"A single value" refers to what the objects represents. Not the number of properties. A typical example would be a Point. It might have X and Y coordinates, but it is conceptually one single value. It is difficult to tell if this applies to your example since we does not know how it is used.

One point with structs is that they use value semantics. I.e. when passed or returned to a method it is copied. To avoid unexpected behaviors due to the copies it is generally recommended to make structs readonly, i.e. immutable. See why mutable structs are evil for more details. For reference, here is the programming guide regarding class vs struct.

Another consideration is how many objects you will have. For things like points where you might have lists of many thousands or even million of points it can be nice to reduce the number of objects the GC have to track. But for hundreds or even a few thousand objects it will probably not matter much.

JonasH
  • 28,608
  • 2
  • 10
  • 23