Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created January 5, 2022 21:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dcomartin/416e18de519b3fcb42ffa6b9ce19ae41 to your computer and use it in GitHub Desktop.
public class InventoryItem : AggregateRoot
{
private bool _activated;
private Guid _id;
private void Apply(InventoryItemCreated e)
{
_id = e.Id;
_activated = true;
}
private void Apply(InventoryItemDeactivated e)
{
_activated = false;
}
public void ChangeName(string newName)
{
if (string.IsNullOrEmpty(newName)) throw new ArgumentException("newName");
ApplyChange(new InventoryItemRenamed(_id, newName));
}
public void Remove(int count)
{
if (count <= 0) throw new InvalidOperationException("cant remove negative count from inventory");
ApplyChange(new ItemsRemovedFromInventory(_id, count));
}
public void CheckIn(int count)
{
if (count <= 0) throw new InvalidOperationException("must have a count greater than 0 to add to inventory");
ApplyChange(new ItemsCheckedInToInventory(_id, count));
}
public void Deactivate()
{
if (!_activated) throw new InvalidOperationException("already deactivated");
ApplyChange(new InventoryItemDeactivated(_id));
}
public override Guid Id
{
get { return _id; }
}
public InventoryItem()
{
// used to create in repository ... many ways to avoid this, eg making private constructor
}
public InventoryItem(Guid id, string name)
{
ApplyChange(new InventoryItemCreated(id, name));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment