❔ Refactoring a Tag class

I have a Tag class that has the ability to store information about tags. Each tag has several properties associated with it. For example:
{
"TagName": "Leveling",
"ValType": 2,
"isScanTag": true,
"isTag": true,
"TagType": 6,
"DBField": "Leveling",
"IsQualityTag": false
}
{
"TagName": "Leveling",
"ValType": 2,
"isScanTag": true,
"isTag": true,
"TagType": 6,
"DBField": "Leveling",
"IsQualityTag": false
}
I am trying to think of a more efficient way to handle this. At the moment, each tag is stored in a json file and then loaded on startup. Therefore, I am accessing each tag with a string, and getting each property based on this file.
public sealed class Tag
{
public string TagName { get; set; }
public ValEnum ValType { get; set; }
public bool IsScanTag { get; set; }
public bool IsTag { get; set; }
public TagTypeEnum TagType { get; set; }
public string DbField { get; set; }
public bool IsQualityTag { get; set; }

public Tag()
{
TagName = "";
IsScanTag = false;
IsTag = false;
TagType = TagTypeEnum.Na;
ValType = ValEnum.VInteger;
DbField = "";
IsQualityTag = false;
}

public enum ValEnum
{
VDouble,
VInteger,
VBoolean,
VLong,
VIntAsDouble,
VByte,
VUInteger
}

public enum TagTypeEnum
{
Na,
First,
Last,
Avg,
AvgBody,
Bit,
PercentageOn
}
}
public sealed class Tag
{
public string TagName { get; set; }
public ValEnum ValType { get; set; }
public bool IsScanTag { get; set; }
public bool IsTag { get; set; }
public TagTypeEnum TagType { get; set; }
public string DbField { get; set; }
public bool IsQualityTag { get; set; }

public Tag()
{
TagName = "";
IsScanTag = false;
IsTag = false;
TagType = TagTypeEnum.Na;
ValType = ValEnum.VInteger;
DbField = "";
IsQualityTag = false;
}

public enum ValEnum
{
VDouble,
VInteger,
VBoolean,
VLong,
VIntAsDouble,
VByte,
VUInteger
}

public enum TagTypeEnum
{
Na,
First,
Last,
Avg,
AvgBody,
Bit,
PercentageOn
}
}
It works, but I think it could be better. I'm running into issues of bringing stuff back into memory with this setup. Any suggestions are welcome, thanks.
18 Replies
MODiX
MODiX3y ago
Ero#1111
REPL Result: Success
sealed record Tag(
string TagName,
Val ValType,
bool IsScanTag,
bool IsTag,
TagType TagType,
string DbField,
bool IsQualityTag);

enum Val
{
VDouble,
VInteger,
VBoolean,
VLong,
VIntAsDouble,
VByte,
VUInteger
}

enum TagType
{
Na,
First,
Last,
Avg,
AvgBody,
Bit,
PercentageOn
}

var json =
"""
{
"TagName": "Leveling",
"ValType": 2,
"isScanTag": true,
"isTag": true,
"TagType": 6,
"DBField": "Leveling",
"IsQualityTag": false
}
""";

var tag = System.Text.Json.JsonSerializer.Deserialize<Tag>(json);
Console.WriteLine(tag);
sealed record Tag(
string TagName,
Val ValType,
bool IsScanTag,
bool IsTag,
TagType TagType,
string DbField,
bool IsQualityTag);

enum Val
{
VDouble,
VInteger,
VBoolean,
VLong,
VIntAsDouble,
VByte,
VUInteger
}

enum TagType
{
Na,
First,
Last,
Avg,
AvgBody,
Bit,
PercentageOn
}

var json =
"""
{
"TagName": "Leveling",
"ValType": 2,
"isScanTag": true,
"isTag": true,
"TagType": 6,
"DBField": "Leveling",
"IsQualityTag": false
}
""";

var tag = System.Text.Json.JsonSerializer.Deserialize<Tag>(json);
Console.WriteLine(tag);
Console Output
Tag { TagName = Leveling, ValType = VBoolean, IsScanTag = False, IsTag = False, TagType = PercentageOn, DbField = , IsQualityTag = False }
Tag { TagName = Leveling, ValType = VBoolean, IsScanTag = False, IsTag = False, TagType = PercentageOn, DbField = , IsQualityTag = False }
Compile: 755.865ms | Execution: 95.777ms | React with ❌ to remove this embed.
ero
ero3y ago
don't see the problem and if you desperately need to keep the names; https://discord.com/channels/143867839282020352/598678594750775301/1042032291871998093
MODiX
MODiX3y ago
thinker227#5176
Try [property: JsonPropertyName("bar")]
Quoted by
<@!542772576905199626> from #roslyn (click here)
React with ❌ to remove this embed.
ero
ero3y ago
sealed record Tag(
string TagName,
Val ValType,
[property: JsonPropertyName("isScanTag")] bool IsScanTag,
[property: JsonPropertyName("isTag")] bool IsTag,
TagType TagType,
[property: JsonPropertyName("DBField")] string DbField,
bool IsQualityTag);
sealed record Tag(
string TagName,
Val ValType,
[property: JsonPropertyName("isScanTag")] bool IsScanTag,
[property: JsonPropertyName("isTag")] bool IsTag,
TagType TagType,
[property: JsonPropertyName("DBField")] string DbField,
bool IsQualityTag);
so like this i guess
Determinism
DeterminismOP3y ago
Well I preferably want to pull all of this out of json But I guess if it's not needed, then I won't
ero
ero3y ago
mh, how do you mean?
Determinism
DeterminismOP3y ago
I was thinking that I could have classes for each property, but I also thought that would be a ton of code
ero
ero3y ago
you really shouldn't care about how much code something takes
Determinism
DeterminismOP3y ago
But I also wanted to group them together In the same list
ero
ero3y ago
if it's better perf, better ux, then that's way more important than "how much code it takes" your question isn't really clear you asked for "a better way to handle this" what's "this"?
Determinism
DeterminismOP3y ago
Making an object, that has a type name, other props, etc. And storing it in a list I wasn't sure if there was a better way to accomplish that
ero
ero3y ago
does this list need to persist between app restarts?
Determinism
DeterminismOP3y ago
It does not
ero
ero3y ago
so why not just a List<Tag>?
Determinism
DeterminismOP3y ago
That's what I have now, I'm assuming what I'm trying to do here is fine. I was just worried that I was creating a mess for anyone after me. Because I have to go through and do stuff like this
status.Scan.ItemByName("Leveling").Value = b.Leveling;
status.Scan.ItemByName("Leveling").Value = b.Leveling;
For each one
ero
ero3y ago
instead of what? looks perfectly fine to me
Determinism
DeterminismOP3y ago
Not sure actually Just looks like a lot when I have 50+ lines of that
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server