A Journey from PHP to .Net Core
A software engineer who works in PHP but now switching toward .Net core. Mostly working on the data layer of an application so as a back-end developer I am developing web services, writing libraries, and do third-party web service/API integrations.
I have worked on different projects but for the last few years, I am working specifically on web services related to the travel industry. I have gained a very deep understanding of a web service/API belongs to one of the most popular giants of the travel industry called Travelport GDS.
So during my integration of Travelport universal API in PHP which is SOAP-based, I learned that the request sends to Travelport via SOAP library can be of two types OOP based or an Array, which is actually mapping internally by SOAP library and construct the desired XML request and send to Travelport and received a response in XML and converted back into object notation rather than return us a raw XML.
I always prefer the OOP way over the Array approach to construct requests as it gives me more flexibility and control over my request construction and off course data encapsulation and prevents me from reinventing the wheel for other requests as some parts of the requests are repeating in other requests.
So for me, the journey from PHP into .Net is not quite difficult despite the fact that C# is a strongly typed language and follows pure OOP concepts, I easily adjust in the .net environment. Before jumping into the .Net world I take a basic course from the plural site C# Fundamentals thanks to David and Uri who make this possible for me.
In this new journey, I decided to share my experience with you guys during development the issues that I faced and their solutions, so I am going to start with my first issue that faced during my request construction and the solution.
Problem 1:
The first problem that I faced in the Travelport UAPI search response is that Travelport returns search response data as a separate entity I mean an isolated object in the same response. You will need to apply logic and combine isolated portions and link them together in order to construct a list of complete flight objects.
So the problem that I faced during the construction of a flight object is a type casting issue. I have to cast the parent object into a child object.
Solution:
Get MasoodRehman’s stories in your inbox
Join Medium for free to get updates from this writer.
In order to cast a parent into a child (down-casting), first of all, I tried a simple solution straight away casting but that did not work. Then I found that we can do down-casting via JSON serialization/de-serialization. So below is the solution that I successfully applied in order to downcast the parent into the child.
using Newtonsoft.Json
var serializedParent = JsonConvert.SerializeObject(parentInstance);
Child c = JsonConvert.DeserializeObject<Child>(serializedParent);
Problem 2:
Another problem that I faced is related to down-casting the same property, which means replacing/overriding a property defined in parent class with the property defined in child class e.g.
The solution for that as IntelliSense suggests us is to use the `new` keyword. I tried that solution but a new problem occurred which is related to the property name collisions. A property `X` in the child class with the property `X` in the parent class during serialization.
In order to resolve this issue, I tried the following solution, and it worked perfectly.
Solution:
The JsonPropertyName attribute actually replaces the parent member with the child member with a new name `MyX` and sets the parent property `X` to null. So in your response, you will see the data will be assigned to `MyX` property and the actual property `X` will be set to null.




