public void REGISTER_PROJECTIONS() { var appendStore = new FileAppendOnlyStore( @"C:\Users\wydev\Documents\GitHub\Clones\lokad-iddd-sample\DomainTests\Store\"); var eventStore = new EventStore(appendStore); var publisher = new DomainEventPublisher(eventStore,0,500); var store = new FileDocumentReaderWriter<CustomerId,CustomerTransactions>(@"C:\Users\wydev\Documents\GitHub\Clones\lokad-iddd-sample\DomainTests\Store\", new ViewStrategy(@"C:\Users\wydev\Documents\GitHub\Clones\lokad-iddd-sample\DomainTests\Views\")); IDocumentWriter<CustomerId,CustomerTransactions> writer = store; var projection = new CustomerTransactionsProjection(writer); publisher.RegisterProjection(projection); var id = new CustomerId(2); var @event = new CustomerCreated { Id = id, Name = "Microsoft", Currency = Currency.Eur }; IList<IEvent> Changes = new List<IEvent>(); Changes.Add(@event); eventStore.AppendToStream(id,0,Changes); publisher.ProcessNewEvents(); }
// 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 Create(CustomerId id, string name, Currency currency, IPricingService service) { if (_state.Created) throw new InvalidOperationException("Customer was already created"); Apply(new CustomerCreated { Created = DateTime.UtcNow, Name = name, Id = id, Currency = currency }); var bonus = service.GetWelcomeBonus(currency); AddPayment("Welcome bonus", bonus); }
public void Create(CustomerId id, string name, Currency currency, IPricingService service) { if (_state.Created) { throw new InvalidOperationException("Customer was already created"); } Apply(new CustomerCreated { Created = DateTime.UtcNow, Name = name, Id = id, Currency = currency }); var bonus = service.GetWelcomeBonus(currency); AddPayment("Welcome bonus", bonus); }
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); }
// 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); } } }