Ejemplo n.º 1
0
        public override string ToString()
        {
            var             performanceCriticalOutputBuilder = new StringBuilder();
            AggregateCommit firstCommit           = aggregateCommits.First();
            string          boundedContext        = firstCommit.BoundedContext;
            string          base64AggregateRootId = Convert.ToBase64String(firstCommit.AggregateRootId);
            string          aggregateName         = Encoding.UTF8.GetString(firstCommit.AggregateRootId).Split('@')[0];

            performanceCriticalOutputBuilder.AppendLine("Aggregate Info");
            performanceCriticalOutputBuilder.AppendLine("==============");
            performanceCriticalOutputBuilder.AppendLine($"- Bounded Context: `{boundedContext}`");
            performanceCriticalOutputBuilder.AppendLine($"- Aggregate root ID (base64): `{base64AggregateRootId}`");
            performanceCriticalOutputBuilder.AppendLine($"- Aggregate name: `{aggregateName}`");
            performanceCriticalOutputBuilder.AppendLine();
            performanceCriticalOutputBuilder.AppendLine("## Commits");

            foreach (var commit in aggregateCommits)
            {
                performanceCriticalOutputBuilder.AppendLine();
                performanceCriticalOutputBuilder.AppendLine($"#### Revision {commit.Revision}");

                foreach (var @event in commit.Events)
                {
                    performanceCriticalOutputBuilder.AppendLine($"- {@event}");
                }
            }
            performanceCriticalOutputBuilder.AppendLine("-------------------------------------------------------------------------------------------------------------------");
            return(performanceCriticalOutputBuilder.ToString());
        }
        public string GetEventsTableName(AggregateCommit aggregateCommit)
        {
            // mynkow if(Environment.GetEnvironmentVariable("ForceCronusChecks"))
            // if (boundedContext)

            var boundedContext = aggregateCommit.BoundedContext;
            return GetEventsTableName(boundedContext);
        }
Ejemplo n.º 3
0
        public void Append(AggregateCommit aggregateCommit)
        {
            if (ReferenceEquals(null, aggregateCommit) == true)
            {
                throw new ArgumentNullException(nameof(aggregateCommit));
            }

            var tenant = tenantResolver.Resolve(aggregateCommit);
            var store  = factory.GetEventStore(tenant);

            store.Append(aggregateCommit);
        }
Ejemplo n.º 4
0
        public async Task AppendAsync(AggregateCommit aggregateCommit)
        {
            AggregateCommit transformedAggregateCommit = aggregateCommit;

            try
            {
                transformedAggregateCommit = aggregateCommitTransformer.Transform(aggregateCommit);
                await eventStore.AppendAsync(transformedAggregateCommit).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex, () => $"Failed to append aggregate with id = {transformedAggregateCommit.AggregateRootId}. \n Exception: {ex.Message}");
                throw;
            }
        }
        public void Append(AggregateCommit aggregateCommit)
        {
            DataTable eventsTable = CreateInMemoryTableForEvents();
            try
            {
                byte[] data = SerializeEvent(aggregateCommit);
                var eventsRow = eventsTable.NewRow();
                eventsRow[0] = Convert.ToBase64String(aggregateCommit.AggregateRootId);
                eventsRow[1] = aggregateCommit.Revision;
                eventsRow[2] = aggregateCommit.Timestamp;
                eventsRow[3] = data;
                eventsTable.Rows.Add(eventsRow);
            }
            catch (Exception ex)
            {
                eventsTable.Clear();
                throw new AggregateStateFirstLevelConcurrencyException("", ex);
            }

            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                connection.Open();
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
                {
                    bulkCopy.DestinationTableName = "[" + tableNameStrategy.GetEventsTableName(aggregateCommit) + "]";
                    bulkCopy.WriteToServer(eventsTable, DataRowState.Added);
                }
            }
            catch (Exception ex)
            {
                throw new AggregateStateFirstLevelConcurrencyException("", ex);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
                else
                {
                    throw new Exception("EventStore bulk persist connection is in strange state.");
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Saves the specified aggregate root.
        /// </summary>
        /// <typeparam name="AR">The type of the aggregate root.</typeparam>
        /// <param name="aggregateRoot">The aggregate root.</param>
        /// <exception cref="Elders.Cronus.DomainModeling.AggregateRootException"></exception>
        public void Save <AR>(AR aggregateRoot) where AR : IAggregateRoot
        {
            if (ReferenceEquals(null, aggregateRoot.UncommittedEvents) || aggregateRoot.UncommittedEvents.Count() == 0)
            {
                return;
            }

            var arCommit = new AggregateCommit(aggregateRoot.State.Id as IBlobId, aggregateRoot.Revision, aggregateRoot.UncommittedEvents.ToList());
            var result   = atomicAction.Execute(aggregateRoot.State.Id, aggregateRoot.Revision, () => eventStore.Append(arCommit));

            if (result.IsSuccessful)
            {
                // #prodalzavameNapred
                // #bravoKobra
                // https://www.youtube.com/watch?v=2wWusHu_3w8
            }
            else
            {
                throw new AggregateStateFirstLevelConcurrencyException("", result.Errors.MakeJustOneException());
            }
        }
 private byte[] SerializeEvent(AggregateCommit commit)
 {
     using (var stream = new MemoryStream())
     {
         serializer.Serialize(stream, commit);
         return stream.ToArray();
     }
 }