Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created January 25, 2023 22:17
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 dcomartin/b335f0b24437d58bfb1b4f60e543836b to your computer and use it in GitHub Desktop.
Save dcomartin/b335f0b24437d58bfb1b4f60e543836b to your computer and use it in GitHub Desktop.
public class Product : Entity, IAggregateRoot
{
public string Name { get; private set; }
public decimal Price { get; private set; }
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
public void SetName(string name)
{
Name = name;
}
public void SetPrice(ProductPrice price)
{
Price = price;
}
}
public record struct ProductPrice
{
private readonly decimal _value;
public ProductPrice(decimal value)
{
if (value <= 0)
{
throw new DomainException("Product price must be greater than zero.");
}
_value = value;
}
public static implicit operator decimal(ProductPrice price)
{
return price._value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment