Search code examples
c#type-conversionslackslack-api

How to make an "independent" (datacontainer)-class in C# holding data from another class? (slack/JSON-related)


My problem is a bit complicated. It is as follows: I get via HTTPClient answers from the (in my case) "Slack"-API. This data is sent as JSON, so I deserialize it into the fitting objects(classes) I wrote for this. Until now I just handled the data directly from these containers. But my boss now said, "what if Slack changes something and these things are called differently all of a sudden? - We need an independent container that provides this received data, always under the same names! So when we implement your .dll we never have to care about these changes, only you have to refactor/rename your classes/members". I totally understand his point. But I can not figure out how I would do this. As I said, these containers are classes I wrote, and when I want to create another container that can take the wanted data and save it, I always stumble over the fact that I cannot convert these object types to the other object types. How could I do that? I tried every way I could imagine and now I am stuck...

Does anybody have an idea or just a tip? Maybe there is a far easier solution to my problem and I can not see it. That's why I wrote what my boss wants from me in this case.

I am sorry for the title of this question, I didn't know what else to write.


Solution

  • You can have slack related property names defined via JsonProperty atttribute. This way you only need to change few strings instead of changing property names and refactoring parts of applications (including maybe UI if you send those objects directly to UI)

    using Newtonsoft.Json;
    [JsonProperty(PropertyName = "SlackPropName")]
    public string MyPropName { get; set; }