Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created May 12, 2020 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/edc260ed58992c53b4e9d8916301d1a6 to your computer and use it in GitHub Desktop.
Save einarwh/edc260ed58992c53b4e9d8916301d1a6 to your computer and use it in GitHub Desktop.
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