I have a RESTful call that can modify the state of an instance. My problem is that the JSON is different based on the type of action the user is taking, it could be a SetProperty
action or a SetState
action (among others).
For example, a POST /api/sample/
_would send the following JSON
{
instanceId: 12312,
actionId: 12345, // Maps to a SetState action
desiredState: 'CURATION'
}
But when calling the same URL for a SetProperty, the JSON would look like
{
instanceId: 12312,
actionId: 25354, // Maps to a SetProperty action
propName: "PropA",
propValue: "user-input"
}
I use the following data contract
[DataContract]
class PostInstanceRequest {
[DataMember]
int instanceId;
[DataMember]
int actionId;
[DataMember]
string desiredState; // Only used for SetState
[DataMember]
string propName; // Only used for SetProperty
[DataMember]
string propValue; // Only used for SetProperty
// Many more action specific properties here...
}
I don't really like this because it feels like I should have a base class for the common properties, and then I should define more base classes with the specific properties.
SetStatePostInstanceRequest
, that addsdesiredState
SetPropertyPostInstanceRequest
, that addspropName
andpropValue
However, if I do that, I don't know what class to deserialize it into. I would have to first deserialize as a PostInstanceRequest
, check the actionId
property, and then deserialize it to the correct subclass.
A possible compromise is to use composition
[DataContract]
class SetStateAction {
[DataMember]
string desiredState;
}
[DataContract]
class SetPropAction {
[DataMember]
string propName;
[DataMember]
string propValue;
}
[DataContract]
class PostInstanceRequest {
[DataMember]
int instanceId;
[DataMember]
int actionId;
[DataMember]
SetPropAction setPropParams;
[DataMember]
SetStateAction setActionParams;
// More references to action specific classes down here
}
Which of the options is going to cause me less trouble? Or is there an even better way that I didn't think of?
2
kinds of values of the same type?