Created
July 19, 2014 19:32
-
-
Save JefClaes/496388c579dc9ce86b56 to your computer and use it in GitHub Desktop.
Is this functional C#?
This file contains hidden or 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
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var item = InventoryItemPure.Register(new InventoryItemName("Domain Driven Design")); | |
| item = InventoryItemPure.CheckIn(item, new InventoryItemCount(4)); | |
| item = InventoryItemPure.CheckOut(item, new InventoryItemCount(3)); | |
| item = InventoryItemPure.Deactivate(item); | |
| Console.ReadLine(); | |
| } | |
| } | |
| public class InventoryItemPure | |
| { | |
| private readonly Guid _id; | |
| private readonly bool _activated; | |
| private InventoryItemName _name; | |
| private InventoryItemCount _count; | |
| public InventoryItemPure(Guid id, bool activated, InventoryItemName name, InventoryItemCount count) | |
| { | |
| Guard.ForNull(name, "name"); | |
| Guard.ForNull(count, "count"); | |
| _id = id; | |
| _activated = activated; | |
| _name = name; | |
| _count = count; | |
| } | |
| public Guid Id { get { return _id; }} | |
| public bool Activated { get { return _activated; }} | |
| public InventoryItemName Name { get { return _name; }} | |
| public InventoryItemCount Count { get { return _count; }} | |
| public static InventoryItemPure CheckIn(InventoryItemPure item, InventoryItemCount count) | |
| { | |
| return new InventoryItemPure(item.Id, item.Activated, item.Name, count.UpBy(count)); | |
| } | |
| public static InventoryItemPure CheckOut(InventoryItemPure item, InventoryItemCount count) | |
| { | |
| return new InventoryItemPure(item.Id, item.Activated, item.Name, count.DownBy(count)); | |
| } | |
| public static InventoryItemPure Deactivate(InventoryItemPure item) | |
| { | |
| return new InventoryItemPure(item.Id, false, item.Name, item.Count); | |
| } | |
| public static InventoryItemPure Register(InventoryItemName name) | |
| { | |
| Guard.ForNull(name, "name"); | |
| return new InventoryItemPure(Guid.NewGuid(), true, name, InventoryItemCount.Empty()); | |
| } | |
| } | |
| public class InventoryItemCount | |
| { | |
| public readonly int _value; | |
| public InventoryItemCount(int value) | |
| { | |
| if (value < 0) throw new ArgumentOutOfRangeException("value", "Item count shouldn't be negative."); | |
| _value = value; | |
| } | |
| public int Value | |
| { | |
| get { return _value; } | |
| } | |
| public InventoryItemCount DownBy(InventoryItemCount count) | |
| { | |
| return new InventoryItemCount(_value - count.Value); | |
| } | |
| public InventoryItemCount UpBy(InventoryItemCount count) | |
| { | |
| return new InventoryItemCount(_value + count.Value); | |
| } | |
| public static InventoryItemCount Empty() | |
| { | |
| return new InventoryItemCount(0); | |
| } | |
| public override string ToString() | |
| { | |
| return _value.ToString(); | |
| } | |
| } | |
| public class InventoryItemName | |
| { | |
| private readonly string _value; | |
| public InventoryItemName(string value) | |
| { | |
| Guard.ForEmpty(value, "value"); | |
| _value = value; | |
| } | |
| public string Value { get { return _value; }} | |
| public override string ToString() | |
| { | |
| return Value; | |
| } | |
| } | |
| public class Guard | |
| { | |
| public static void ForNull(object o, string description) | |
| { | |
| if (o == null) | |
| throw new ArgumentNullException(description); | |
| } | |
| public static void ForEmpty(string value, string description) | |
| { | |
| if (string.IsNullOrEmpty(value)) | |
| throw new ArgumentNullException(description); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment