Currently I have 3 different classes for the same type of entity. One is the main class that contains logic and everything the entity needs. Then I have a DB object, that has the same structure as the entity inside the database. The third is a data transfer object (DTO) that only contains the values the client is supposed to know about.
Now whenever I add a variable, I have to add the variable in all 3 classes and copy the DTO class over to the client too. Also, I have to add the variable to each converter method, that creates the DTO and DB objects from the main object and need to add a check to the DTO converter, so that I can differentiate which variables actually need to be updated to lessen the network usage. It's a huge mess and the bigger those classes become, the more I hate my current approach.
What's a smarter approach to this? Maybe there is a way to use C# attributes, but before I try to learn about how to make my own and implement it, I'd love to know if it's even a good choice.
edit 1:
following method checks which values have been updated and not sent to the client yet and returns a new DTO object to send, if the client is not up-to-date. The DTO object is serialized to json and sent as a string. Then its deserialized on the client and the client checks for valid values to know which one to update:
public CharacterDTO GetNetworkDTOValues()
{
bool shouldUpdate = false;
CharacterDTO returnDTO = new CharacterDTO();
if (_clientInstance == null)
{
_clientInstance = new CharacterDTO();
}
if (_clientInstance.Level != Level)
{
returnDTO.Level = Level;
shouldUpdate = true;
}
if (_clientInstance.Experience != Experience)
{
returnDTO.Experience = Experience;
shouldUpdate = true;
}
if (_clientInstance.Life != Life)
{
returnDTO.Life = Life;
shouldUpdate = true;
}
[...] (there are around 30 more variables)
if (shouldUpdate)
{
_clientInstance = CreateDTO(this);
return returnDTO;
}
return null;
}