The problem I had with APIs and Inheritance in .NET

The problem I had with APIs and Inheritance in .NET

And how to easily solve it

Photo by rupixen.com on Unsplash

So here’s the problem I had. I had an endpoint in my OrderController that had to add an order, similar to the following (I will be using a short example for the purpose of keeping this short and sweet):

https://medium.com/media/611a7075b056fba419d27a33f33903ed/href

With my OrderEntity looking something like this:

https://medium.com/media/335508ee87b523c52c1052ac8bd0dd10/href

Fairly straightforward up until now. The problem comes with those PaymentDetails on the last line with the class looking like this:

https://medium.com/media/e887c3ecbc53ec86fbb07c5f99bebd48/href

Fear not, there is a reason for it to be empty! The idea behind it was that there would be multiple different payment methods, containing different properties that would inherit this class. In this case we have two:

https://medium.com/media/069474e6cb65783782d184b322ea2713/hrefhttps://medium.com/media/84de1f88762d16a269f6e5876297fa69/href

Right, the thinking behind the was that depending on the JSON body i pass to my controller, it would determine which payment method I’m using. So, say I sent something like this:

https://medium.com/media/72196dc9aa446b7d8b42a9b20fd027f5/href

I would’ve expected the API to be able to pick up that i’m sending an order with a payment method that should be Braintree. Unfortunately, all I got was empty PaymentDetails, which in all fairness does make some sense, but it didn’t occur to me that .NET won’t support inheritance in the APIs like I was expected.

Now, the solution. There was a lot of ideas and solutions in the internet, most of them either, quite frankly hacks, or just messy and not scalable. For example, you could add all those different Payment Details on the actual Order class and just pass on the one being used, but that just wouldn’t cut it, especially if you have a high number of methods, so the best solution (Or at least it looks like the best) was something along the way:

https://medium.com/media/262faf2d43655246ff2399f6971daa41/href

What happens here is, we are using the StreamReader to get the data from the JSON passed to the controller and then we can map it to the OrderEntity. The difference maker is the fact that we can use that initial JSON and parse it in order to convert the payment details from the request and then depending on the PaymentTypeId convert the data to the appropriate object.

By no means do I think this is the perfect solution, however it is one that works fine in this case, but if anyone has any suggestions — I am all ears.


The problem I had with APIs and Inheritance in .NET was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Ivan Stoev

The problem I had with APIs and Inheritance in .NET

And how to easily solve it

Photo by rupixen.com on Unsplash

So here's the problem I had. I had an endpoint in my OrderController that had to add an order, similar to the following (I will be using a short example for the purpose of keeping this short and sweet):

With my OrderEntity looking something like this:

Fairly straightforward up until now. The problem comes with those PaymentDetails on the last line with the class looking like this:

Fear not, there is a reason for it to be empty! The idea behind it was that there would be multiple different payment methods, containing different properties that would inherit this class. In this case we have two:

Right, the thinking behind the was that depending on the JSON body i pass to my controller, it would determine which payment method I'm using. So, say I sent something like this:

I would've expected the API to be able to pick up that i'm sending an order with a payment method that should be Braintree. Unfortunately, all I got was empty PaymentDetails, which in all fairness does make some sense, but it didn't occur to me that .NET won't support inheritance in the APIs like I was expected.

Now, the solution. There was a lot of ideas and solutions in the internet, most of them either, quite frankly hacks, or just messy and not scalable. For example, you could add all those different Payment Details on the actual Order class and just pass on the one being used, but that just wouldn't cut it, especially if you have a high number of methods, so the best solution (Or at least it looks like the best) was something along the way:

What happens here is, we are using the StreamReader to get the data from the JSON passed to the controller and then we can map it to the OrderEntity. The difference maker is the fact that we can use that initial JSON and parse it in order to convert the payment details from the request and then depending on the PaymentTypeId convert the data to the appropriate object.

By no means do I think this is the perfect solution, however it is one that works fine in this case, but if anyone has any suggestions — I am all ears.


The problem I had with APIs and Inheritance in .NET was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Ivan Stoev


Print Share Comment Cite Upload Translate Updates
APA

Ivan Stoev | Sciencx (2022-01-17T02:27:37+00:00) The problem I had with APIs and Inheritance in .NET. Retrieved from https://www.scien.cx/2022/01/17/the-problem-i-had-with-apis-and-inheritance-in-net/

MLA
" » The problem I had with APIs and Inheritance in .NET." Ivan Stoev | Sciencx - Monday January 17, 2022, https://www.scien.cx/2022/01/17/the-problem-i-had-with-apis-and-inheritance-in-net/
HARVARD
Ivan Stoev | Sciencx Monday January 17, 2022 » The problem I had with APIs and Inheritance in .NET., viewed ,<https://www.scien.cx/2022/01/17/the-problem-i-had-with-apis-and-inheritance-in-net/>
VANCOUVER
Ivan Stoev | Sciencx - » The problem I had with APIs and Inheritance in .NET. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/17/the-problem-i-had-with-apis-and-inheritance-in-net/
CHICAGO
" » The problem I had with APIs and Inheritance in .NET." Ivan Stoev | Sciencx - Accessed . https://www.scien.cx/2022/01/17/the-problem-i-had-with-apis-and-inheritance-in-net/
IEEE
" » The problem I had with APIs and Inheritance in .NET." Ivan Stoev | Sciencx [Online]. Available: https://www.scien.cx/2022/01/17/the-problem-i-had-with-apis-and-inheritance-in-net/. [Accessed: ]
rf:citation
» The problem I had with APIs and Inheritance in .NET | Ivan Stoev | Sciencx | https://www.scien.cx/2022/01/17/the-problem-i-had-with-apis-and-inheritance-in-net/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.