I'm developing a distributed application (server and clients). There are some instructions sent by the server (e.g., InitCommand, StartCommand, StopCommand). Every command has its own arguments. Now if it's coming to serialization and networking, I'm asking myself whether I should create a sub-class for every command type (which inherits from Command) or just put all possible arguments in the Command class.
When handling a command it is if (command is InitCommand) vs if (command.Type == CommandType.Init).
My first implementation uses the first option. I'm serializing by hand and put the type information at the beginning. Now if I receive data (byte[]) from network, I firstly check the actual type (deserialize only the first k bytes) and secondly use some deserialization methods (e.g., Serializer.Deserialize<InitCommand>).
The second option seems much easier to use, because I can use automated serialization and don't need to check the type before deserialization. But it annoys me, that I have to put all arguments into one class.
What do you think is better, or do you know about other approaches?
It sounds like you should have one class with multiple methods; Start, Init, Stop. This would support everything in one class. SqlCommand has a CommandType of Text StoredProcedure, and TableDirect and that sounds like what you're trying to rebuild for a different type of command. I would choose #2 and look at how Microsoft Implemented SqlCommand with CommandType.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With