Example #1
0
        //Immutable class pattern - Order checks its invariant data at construction, state changes must
        //happen through methods not just property setters to ensure that the Order object is always
        //in a valid state. Factory methods used because they have more flexibility than constructors
        //with exception raising, virtual calls, etc
        public static Order CreateNew(Customer customer, string address1, string address2,
                                      string country, string phone, string stateAbbreviation, string zipCode, decimal taxRate)
        {
            Guard.ForNull(customer, "customer");
            Guard.ForNullOrEmpty(address1, "address1");
            Guard.ForNullOrEmpty(country, "country");
            Guard.ForNullOrEmpty(phone, "phone");
            Guard.ForNullOrEmpty(stateAbbreviation, "stateAbbreviation");
            Guard.ForNullOrEmpty(zipCode, "zipCode");

            Guard.ForNullOrEmpty(stateAbbreviation, "stateAbbreviation");
            var foundState = States.GetByAbbreviation(stateAbbreviation);

            Guard.ForNull(foundState, "stateAbbreviation", "Invalid state abbreviation");

            Order order = CreateOrder(customer, taxRate);

            order.Id          = Guid.NewGuid();
            order.State       = SaveState.UnSaved;
            order.OrderStatus = OrderStatus.Created;


            //Double Dispatch pattern to create BillingAddress - Billing only has reference to an order ID
            //imposing a traversal direction that is not bi-directional (not needed for this object)
            //BillingAddress has its own Guard/invariant checks
            order.BillingAddress = BillingAddress.Create(order, address1, address2, country, phone,
                                                         stateAbbreviation, zipCode);

            //domain events added for any event sourcing needs - are published to consumers after save transaction completes
            order.AddEvent(new OrderCreatedEvent(order));
            return(order);
        }
Example #2
0
        public static Order CreateExisting(Guid id, Customer customer, BillingAddress billing,
                                           decimal taxRate, OrderStatus status)
        {
            Guard.ForNullOrEmpty(id, "id");
            Guard.ForNull(billing, "billing");

            Order order = CreateOrder(customer, taxRate);

            order.Id             = id;
            order.State          = SaveState.Saved;
            order.OrderStatus    = status;
            order.BillingAddress = billing;

            order.AddEvent(new OrderHydratedEvent(order));

            return(order);
        }
Example #3
0
        public static BillingAddress Create(Order order, string dtoAddress1, string dtoAddress2, string dtoCountry
                                            , string dtoPhone, string dtoStateAbbreviation, string dtoZipCode)
        {
            //add guards

            BillingAddress ba = new BillingAddress();

            ba.Id    = Guid.NewGuid();
            ba.State = SaveState.UnSaved;

            ba.Address1          = dtoAddress1;
            ba.Address2          = dtoAddress2;
            ba.Country           = dtoCountry;
            ba.Phone             = dtoPhone;
            ba.StateAbbreviation = dtoStateAbbreviation;
            ba.ZipCode           = dtoZipCode;

            ba.OrderId = order.Id;

            ba.AddEvent(new BillingAddressCreatedEvent(ba));

            return(ba);
        }
Example #4
0
 public BillingAddressCreatedEvent(BillingAddress billingAddress)
 {
     BillingAddress        = billingAddress;
     DateTimeEventOccurred = DateTime.Now;
 }