1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6
7 usingHappy.Domain;8 usingHappy.Domain.Tree;9 usingHappy.Infrastructure.ExtentionMethods;10 usingHappy.Example.Events.TestOrders;11
12 namespaceHappy.Example.Domain.TestOrders13 {14 public partial class TestOrder : AggregateRoot
15 {16 privateITestOrderState _orderState;17
18 protectedTestOrder() { }19
20 public TestOrder(string orderCode, stringcustomer)21 {22 orderCode.MustNotNullAndNotWhiteSpace("orderCode");23 customer.MustNotNullAndNotWhiteSpace("customer");24
25 this.Id =Guid.NewGuid();26 this.OrderCode =orderCode;27 this.Customer =customer;28 this.OrderState = TestOrderStateFactory.CreateUnCommittedTestOrderState(this);29 this.TestOrderDetails = new List();30 }31
32 public virtual System.String OrderCode { get; protected set; }33 public virtual System.String Customer { get; protected set; }34 public virtual System.Decimal Total { get; protected set; }35 public virtual System.String Status { get; protected set; }36 public virtual ICollection TestOrderDetails { get; protected set; }37
38 privateITestOrderState OrderState39 {40 get
41 {42 if (_orderState == null)43 {44 _orderState = TestOrderStateFactory.Create(this, this.Status);45 }46
47 return_orderState;48 }49 set
50 {51 _orderState =value;52 this.Status =value.Status;53 }54 }55
56 public voidAddTestOrderDetail(TestOrderDetail testOrderDetail)57 {58 this.OrderState.AddTestOrderDetail(testOrderDetail);59 }60
61 public void UpdateTestOrderDetail(Guid testOrderDetailId, decimalsubtotal)62 {63 this.OrderState.UpdateTestOrderDetail(testOrderDetailId, subtotal);64 }65
66 public voidRemoveTestOrderDetail(Guid testOrderDetailId)67 {68 this.OrderState.RemoveTestOrderDetail(testOrderDetailId);69 }70
71 public voidCommit()72 {73 this.OrderState.Commit();74 }75
76 public voidVerify()77 {78 this.OrderState.Verify();79 }80
81 private voidDoAddTestOrderDetail(TestOrderDetail testOrderDetail)82 {83 this.TestOrderDetails.Add(testOrderDetail);84
85 this.Total +=testOrderDetail.Subtotal;86 }87
88 private void DoUpdateTestOrderDetail(Guid testOrderDetailId, decimalsubtotal)89 {90 var testOrderDetail = this.TestOrderDetails.First(x => x.Id ==testOrderDetailId);91
92 this.Total -=testOrderDetail.Subtotal;93 testOrderDetail.Subtotal =subtotal;94 this.Total +=testOrderDetail.Subtotal;95 }96
97 private voidDoRemoveTestOrderDetail(Guid testOrderDetailId)98 {99 var testOrderDetail = this.TestOrderDetails.First(x => x.Id ==testOrderDetailId);100
101 this.TestOrderDetails.Remove(testOrderDetail);102 this.Total -=testOrderDetail.Subtotal;103 }104
105 private voidDoCommit()106 {107 this.OrderState = TestOrderStateFactory.CreatePendingVerificationTestOrderState(this);108 }109
110 private voidDoVerify()111 {112 this.OrderState = TestOrderStateFactory.CreateVerifiedTestOrderState(this);113
114 this.PublishEvent(newTestOrderVerified());115 }116 }117 }