/InventoryItem.cs Secret
Created
January 5, 2022 21:56
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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