I have a class that is automatically generated from XML and contains a property Item
which can be of different types based on the OrderType
enum. Here’s a simplified structure:
public class GeneratedFromXml
{
public string Name { get; set; }
public OrderType OrderType { get; set; }
[XmlElement("Service", typeof(TransportElement), IsNullable = true)]
[XmlElement("Transport", typeof(Transport), IsNullable = true)]
public object Item { get; set; }
}
public class Transport
{
public TransportElement PickUp { get; set; }
public TransportElement Delivery { get; set; }
}
public class TransportElement
{
public Address Address { get; set; }
public DateTime? ArrivalTime { get; set; }
// other properties
}
Depending on the OrderType
, Item
can be either a Transport
(containing two TransportElement
instances) or a single TransportElement
. I need to map this structure to an interface that will be implemented by a class exposed to an API.
My idea was to use an array of TransportElement
, which could have one or two elements based on OrderType
but I’m open to other suggestions.
Here is an example of how the MappingService
is accessed in my existing application. Though this particular FromXml
class is not nested and doesn’t contain any dynamic objects.
private IObservable<IMappingResult<FromXml, IForApi, CustomData>> Map(FromXml input)
{
return MappingService.MapAsync<IMappingResult<FromXml, IForApi, CustomData>>(input);
}
I’ve only just started using AutoMapper
and this is a little beyond my knowledge. Any help is appreciated.