public void When_no_authorization_rules_have_been_configured_for_a_given_command_then_checks_are_unauthorized() { var customer = new Customer(); var account = new CustomerAccount(); var addEmail = new ChangeEmailAddress(); customer.IsAuthorizedTo(addEmail, account) .Should().Be(false); }
public ShippingResult ShipOrder(Customer customer, Order order, Destination destination) { //can only ship via ground to the customer's own address if (new GroundShippingApprovalRule(destination).IsSatisfiedBy(customer)) //do something here with customer, order, and destination return ShippingResult.Success; else return ShippingResult.Failure; }
public ShippingResult ShipOrder(Customer customer, Order order, Destination destination) { //can only ship via air if not in a too-nearby state if (new AirShippingApprovalRule(destination).IsSatisfiedBy(customer)) //do something here with customer, order, and destination return ShippingResult.Success; else return ShippingResult.Failure; }
public void Entities_Are_Equal() { Customer customer = new Customer(); Customer otherCustomer = new Customer(); SetInstanceFieldValue(customer, "_persistenceId", 1); SetInstanceFieldValue(otherCustomer, "_persistenceId", 1); Assert.That(customer, Is.EqualTo(otherCustomer)); }
// method with direct call, as illustrated in the IDDD Book // Step 1: LockCustomerForAccountOverdraft method of // Customer Application Service is called public void LockCustomerForAccountOverdraft(CustomerId customerId, string comment) { // Step 2.1: Load event stream for Customer, given its id var stream = _eventStore.LoadEventStream(customerId); // Step 2.2: Build aggregate from event stream var customer = new Customer(stream.Events); // Step 3: call aggregate method, passing it arguments and pricing domain service customer.LockForAccountOverdraft(comment, _pricingService); // Step 4: commit changes to the event stream by id _eventStore.AppendToStream(customerId, stream.Version, customer.Changes); }
public void AuthorizationPolicy_For_returns_an_instance_based_on_the_actual_rather_than_declared_type_of_the_principal() { AuthorizationFor<Customer>.ToApply<Cancel>.ToA<Order> .Requires((a, b, c) => true); IPrincipal iprincipal = new Customer(); var customer = new Customer(); var cancel = new Cancel(); var order = new Order(); iprincipal.IsAuthorizedTo(cancel, order).Should().Be(true); customer.IsAuthorizedTo(cancel, order).Should().Be(true); }
public void ShipOrder_Can_Return_Success() { Customer customer = new Customer(); Order order = new Order(); Destination destination = new Destination("123", "Main Street", "Anywhere", new State("SomeState", "SS"), "12345"); customer.ChangeAddress(destination); IShipOrders groundShipper = new GroundShipper(); Assert.That(groundShipper.ShipOrder(customer, order, destination), Is.EqualTo(ShippingResult.Success)); }
public void Entities_Are_Equal() { Customer customer = new Customer(); Name name = new Name("Steve", "Bohlen"); SetInstanceFieldValue(customer, "_persistenceId", 1); customer.ChangeName(name); Customer otherCustomer = new Customer(); Name differentName = new Name(String.Format("{0}_DIFFERENT", customer.Firstname), String.Format("{0}_DIFFERENT", customer.Lastname)); SetInstanceFieldValue(otherCustomer, "_persistenceId", 1); otherCustomer.ChangeName(differentName); Assert.That(customer, Is.EqualTo(otherCustomer)); }
public void Can_Get_Customers_By_State() { //new repository ICustomerRepository customers = new Customers(); //a state that *is* NY var ny = new State("New York", "ny"); //an address in NY var nyAddress = new Address("1", "Main Street", "New York", ny, "12345"); //a state *not* NY var notNy = new State("New Jersey", "nj"); //an address *not* in NY var notNyAddress = new Address("1", "Main Street", "Trenton", notNy, "12345"); //a customer not in NY var notNyCustomer1 = new Customer(); notNyCustomer1.ChangeAddress(notNyAddress); //a customer in NY var nyCustomer1 = new Customer(); nyCustomer1.ChangeAddress(nyAddress); //another customer in NY var nyCustomer2 = new Customer(); nyCustomer2.ChangeAddress(nyAddress); //still one more customer in NY var nyCustomer3 = new Customer(); nyCustomer3.ChangeAddress(nyAddress); //add all the customers to the repository customers.AddCustomer(nyCustomer1); customers.AddCustomer(nyCustomer2); customers.AddCustomer(nyCustomer3); customers.AddCustomer(notNyCustomer1); //select customers from the state that is NY var results = customers.FromState(ny); Assert.That(results, Has.Member(nyCustomer1)); Assert.That(results, Has.Member(nyCustomer2)); Assert.That(results, Has.Member(nyCustomer3)); Assert.That(results.Count(), Is.EqualTo(3)); Assert.That(results, Has.No.Member(notNyCustomer1)); }
public void IsDenied_overrides_other_authorizations() { AuthorizationFor<Customer>.ToApply<SuspendAccount>.ToA<CustomerAccount>.IsDenied(); AuthorizationFor<Customer>.ToApplyAnyCommand.ToA<CustomerAccount>.Requires((c1, c2) => c1.Id == c2.Id); Guid customerId = Guid.NewGuid(); var customerAccount = new CustomerAccount(customerId); var customerPrincipal = new Customer { Id = customerId, IsAuthenticated = true }; customerPrincipal.IsAuthorizedTo(new ChangeEmailAddress(), customerAccount) .Should().BeTrue(); customerPrincipal.IsAuthorizedTo(new SuspendAccount(), customerAccount) .Should().BeFalse(); }
public void The_event_Actor_is_set_from_the_Command_Principal() { // arrange var customer = new Customer(Any.FullName()); var serviceRepresentative = new CustomerServiceRepresentative { Name = Any.FullName() }; var command = new SpecifyShippingInfo { Principal = serviceRepresentative }; var order = new Order(new CreateOrder(customer.Name) { Principal = customer }); // act order.Apply(command); // assert order.Events() .OfType<Order.Created>() .Single() .Actor() .Should() .Be(customer.Name); order.Events() .OfType<Order.ShippingMethodSelected>() .Single() .Actor() .Should() .Be(serviceRepresentative.Name); }
public void _TestSetUp() { _invaldiName = new Name(VALID_FIRSTNAME, INVALID_LASTNAME); _customer = new Customer(); }
public void IsDenied_does_not_need_to_specify_a_resource_type_for_the_command() { AuthorizationFor<Customer>.ToApply<UnsuspendAccount>.IsDenied(); AuthorizationFor<Customer>.ToApplyAnyCommand.ToA<CustomerAccount>.Requires((c1, c2) => c1.Id == c2.Id); Guid customerId = Guid.NewGuid(); var customerAccount = new CustomerAccount(customerId); var customerPrincipal = new Customer { Id = customerId, IsAuthenticated = true }; customerPrincipal.IsAuthorizedTo(new ChangeEmailAddress(), customerAccount) .Should().BeTrue(); customerPrincipal.IsAuthorizedTo(new UnsuspendAccount(), customerAccount) .Should().BeFalse(); }
public void _TestSetUp() { _validName = new Name(VALID_FIRSTNAME, VALID_LASTNAME); _customer = new Customer(); }
public bool NewOrderWouldExceedCreditLimit(Customer customer, Order order) { //TODO: contact back-end system for actual response return false; }
public bool IsWithinCreditLimitByMargin(Customer customer, Currency margin) { //TODO: contact back-end system for actual response return true; }
// Sample of method that would apply simple conflict resolution. // see IDDD book or Greg's videos for more in-depth explanation void UpdateWithSimpleConflictResolution(CustomerId id, Action<Customer> execute) { while (true) { EventStream eventStream = _eventStore.LoadEventStream(id); Customer customer = new Customer(eventStream.Events); execute(customer); try { _eventStore.AppendToStream(id, eventStream.Version, customer.Changes); return; } catch (OptimisticConcurrencyException ex) { foreach (var clientEvent in customer.Changes) { foreach (var actualEvent in ex.ActualEvents) { if (ConflictsWith(clientEvent, actualEvent)) { var msg = string.Format("Conflict between {0} and {1}", clientEvent, actualEvent); throw new RealConcurrencyException(msg, ex); } } } // there are no conflicts and we can append _eventStore.AppendToStream(id, ex.ActualVersion, customer.Changes); } } }
public Currency RemainingCredit(Customer customer) { //TODO: contact back-end system for actual response return new Currency(200.0); }
void Update(CustomerId id, Action<Customer> execute) { // Load event stream from the store EventStream stream = _eventStore.LoadEventStream(id); // create new Customer aggregate from the history Customer customer = new Customer(stream.Events); // execute delegated action execute(customer); // append resulting changes to the stream _eventStore.AppendToStream(id, stream.Version, customer.Changes); }
public bool IsWithinCreditLimit(Customer customer) { //TODO: contact back-end system for actual response return true; }
public void ToApplyAnyCommand_allows_an_authorization_rule_to_be_declared_for_all_commands_for_a_given_resource_type() { var principals = new List<IPrincipal>(); var resources = new List<EventSourcedAggregate>(); AuthorizationFor<Customer>.ToApplyAnyCommand.ToA<Order> .Requires((principal, resource) => { principals.Add(principal); resources.Add(resource); return true; }); var customer = new Customer(); var order1 = new Order(); var order2 = new Order(); customer.IsAuthorizedTo(new Cancel(), order1) .Should().BeTrue(); customer.IsAuthorizedTo(new Place(), order2) .Should().BeTrue(); principals.Should().Contain(customer); resources.Should().Contain(order1); resources.Should().Contain(order2); }