CSharp · Console
using System;
public class Program
{
public static void Main()
{
var parser = new ThirdPartyOrderParser();
var order = parser.ParseOrder("text");
if (order is ThirdPartyOrderV1)
{
IngestOrderV1(order as ThirdPartyOrderV1);
}
else if (order is ThirdPartyOrderV2)
{
IngestOrderV2(order as ThirdPartyOrderV2);
}
else if (order is ThirdPartyOrderV3)
{
IngestOrderV3(order as ThirdPartyOrderV3);
}
}
private static void IngestOrderV1(ThirdPartyOrderV1 order)
{
Console.WriteLine("Order total: " + order.Total);
// ...
}
private static void IngestOrderV2(ThirdPartyOrderV2 order)
{
Console.WriteLine("Order total: " + order.Total);
// ...
}
private static void IngestOrderV3(ThirdPartyOrderV3 order)
{
Console.WriteLine("Order total: " + order.Total);
// ...
}
}
// Order V1
public class ThirdPartyOrderV1
{
public decimal Total { get; set; }
public ThirdPartyOrderItemV1[] LineItems { get; set; }
public ThirdPartyOrderAddressV1 BillingAddress { get; set; }
public ThirdPartyOrderAddressV1 ShippingAddress { get; set; }
public string SomeV1ThingIDontCareAbout { get; set; }
public decimal AnotherV1ThingIDontCareAbout { get; set; }
// ...
}
public class ThirdPartyOrderItemV1
{
public int ItemID { get; set; }
public int Quantity { get; set; }
// ...
}
public class ThirdPartyOrderAddressV1
{
public string Name { get; set; }
public string Street { get; set; }
// ...
}
// Order V2
public class ThirdPartyOrderV2
{
public decimal Total { get; set; }
public ThirdPartyOrderItemV2[] LineItems { get; set; }
public ThirdPartyOrderAddressV2 BillingAddress { get; set; }
public ThirdPartyOrderAddressV2 ShippingAddress { get; set; }
public object SomeV2ThingIDontCareAbout { get; set; }
// ...
}
public class ThirdPartyOrderItemV2
{
public int ItemID { get; set; }
public int Quantity { get; set; }
// ...
}
public class ThirdPartyOrderAddressV2
{
public string Name { get; set; }
public string Street { get; set; }
// ...
}
// Order V3
public class ThirdPartyOrderV3
{
public decimal Total { get; set; }
public ThirdPartyOrderItemV3[] LineItems { get; set; }
public ThirdPartyOrderAddressV3 BillingAddress { get; set; }
public ThirdPartyOrderAddressV3 ShippingAddress { get; set; }
// ...
}
public class ThirdPartyOrderItemV3
{
public int ItemID { get; set; }
public int Quantity { get; set; }
public string Metadata { get; set; }
// ...
}
public class ThirdPartyOrderAddressV3
{
public string Name { get; set; }
public string Street { get; set; }
// ...
}
public class ThirdPartyOrderParser
{
public object ParseOrder(string text)
{
return new ThirdPartyOrderV1
{
Total = 5
};
}
}