I am reading existing code. I noticed that there are many data object files which have a struct and a class together to define a data object. Like the following one: do you think it is a good style?
In ONE file:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct LaneDataStruct
{
public ushort red;
public ushort yellow;
public ushort green;
public ushort blue;
public ushort orange;
}
public class LaneData
{
private LaneDataStruct laneDataStruct;
public LaneData(ushort red, ushort yellow, ushort green, ushort blue, ushort orange)
{
this.laneDataStruct.red = red;
this.laneDataStruct.yellow = yellow;
this.laneDataStruct.green = green;
this.laneDataStruct.blue = blue;
this.laneDataStruct.orange = orange;
}
public LaneData(ushort[] values)
{
this.laneDataStruct.red = values[0];
this.laneDataStruct.yellow = values[1];
this.laneDataStruct.green = values[2];
this.laneDataStruct.blue = values[3];
this.laneDataStruct.orange = values[4];
}
public LaneData(LaneDataStruct laneDataStruct)
{
this.laneDataStruct = laneDataStruct;
}
public LaneDataStruct getLaneDataStruct()
{
return this.laneDataStruct;
}
public string toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("LaneData.red=" + this.getLaneDataStruct().red + "\n");
stringBuilder.Append("LaneData.yellow=" + this.getLaneDataStruct().yellow + "\n");
stringBuilder.Append("LaneData.green=" + this.getLaneDataStruct().green + "\n");
stringBuilder.Append("LaneData.blue=" + this.getLaneDataStruct().blue + "\n");
stringBuilder.Append("LaneData.orange=" + this.getLaneDataStruct().orange);
return stringBuilder.ToString();
}
}
I would only call this a good practice in an exotic P/Invoke scenario, and even then it’s questionable.
First, the struct is poorly designed since it is mutable. See this question for more information.
The class looks like it was written to address the fact that in C#, there is no deterministic way to initialize a structure. You can’t give a struct a default constructor, and you can’t cause a non-default constructor to be called. So it looks like the class wraps the structure and gives a guarantee that the structure will be initialized to some state. However, it does no validation so I strongly doubt there is any value to it at all.
This would only be “useful” if
LaneDataStructrepresents a structure that is used for P/Invoke (the LayoutKind attribute is a hint this may be true) and that can’t have certain values. It would still be preferable to give it properties that perform validation and make the fields private. Usually, P/Invoke code is written in a separate layer where it’s considered acceptable to have warts like “make sure to initialize this struct after creating one”.