Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A second variation of the shopping cart DTOs using custom attributes.
abstract class ShoppingCart
{
public ShoppingCart(string state)
{
State = state;
}
[JsonProperty("_state")]
public string State { get; }
}
class EmptyCart : ShoppingCart
{
public EmptyCart() : base("empty") {}
}
class ActiveCart : ShoppingCart
{
public ActiveCart() : base("paid") { }
[JsonProperty("unpaidItems")]
public Item[] UnpaidItems { get; set; }
}
class PaidCart : ShoppingCart
{
public PaidCart() : base("paid") {}
[JsonProperty("paidItems")]
public Item[] PaidItems { get; set; }
[JsonProperty("payment")]
public Money Payment { get; set; }
[JsonProperty("timestamp")]
public string Timestamp { get; set; }
}
class Item
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
public string Description { get; set; }
}
class Money
{
[JsonProperty("amount")]
public double Amount { get; set; }
[JsonProperty("currency")]
public string Currency { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment