Let's consider a domain abstract class for an order history and concrete classes considering events such like payments, cancellations, reactivations etc (the following code is a VERY simplified version)
public abstract class OrderEvent
{
protected OrderEvent(DateTime eventDate)
{
EventDate = eventDate;
}
public abstract string Description { get; }
public DateTime EventDate { get; protected set; }
}
public class CancellationEvent : OrderEvent
{
public CancellationEvent(DateTime cancelDate)
: base(cancelDate)
{
}
public override string Description { get { return "Cancellation"; } }
}
public class PaymentEvent : OrderEvent
{
public PaymentEvent(DateTime eventDate, decimal amount, PaymentOption paymentOption) : base(eventDate)
{
Description = description;
Amount = amount;
PaymentOption = paymentOption;
}
public override string Description { get{ return "Payment"; } }
public decimal Amount { get; protected set; }
public PaymentOption PaymentOption { get; protected set; }
}
Now I have to build a ViewModel for my ASP.NET MVC project upon this domain model that will encapsulate all the events into a single class for a grid exhibition on the view.
public class OrderHistoryViewModel
{
public OrderHistoryViewModel(OrderEvent orderEvent)
{
// Here's my doubt
}
public string Date { get; protected set; }
public string Description { get; protected set; }
public string Amount { get; protected set; }
}
How can I access the specific properties from concrete classes, like the Amount property on PaymentEvent without doing some smelly thing like switch or if?
Thanks!