コード例 #1
0
 /// <summary>
 /// Check the validity of an event (i.e. all required fields are set).
 /// </summary>
 /// <param name="event">Event to check</param>
 /// <exception cref="DomainEventIncompleteException">The domain event is incomplete/invalid and cannot be added to the event store.</exception>
 private void CheckValidity(IDomainEvent @event)
 {
     if ([email protected]())
     {
         throw new DomainEventIncompleteException();
     }
 }
コード例 #2
0
        public async Task <DomainEventResult> AppendAsync(
            IDomainEvent @event,
            CancellationToken token = default)
        {
            if ([email protected]())
            {
                return(DomainEventResult.StorageFailed(@event.Id, $"The domain event: [{@event.Print()}] cannot be stored mysql state backend."));
            }

            var domainEventRecord = DomainEventRecordPortAdapter.ToDomainEventRecord(@event, _binarySerializer);
            var parameters        = DbParameterProvider.ReflectionParameters(domainEventRecord);

            var tables = _options.Tables.DomainEventOptions;
            var insertDomainEventIndexSql = $"INSERT INTO `{tables.DomainEventIndices}`(`DomainCommandId`,`DomainCommandType`,`DomainCommandVersion`,`AggregateRootId`,`AggregateRootType`,`AggregateRootVersion`,`AggregateRootGeneration`,`DomainEventId`,`DomainEventType`,`DomainEventVersion`,`DomainEventPayloadBytes`,`CreatedTimestamp`) VALUES(@DomainCommandId,@DomainCommandType,@DomainCommandVersion,@AggregateRootId,@AggregateRootType,@AggregateRootVersion,@AggregateRootGeneration,@DomainEventId,@DomainEventType,@DomainEventVersion,@DomainEventPayloadBytes,@CreatedTimestamp)";
            var insertDomainEventSql      = $"INSERT INTO `{tables.DomainEvents}`(`DomainEventId`,`Payload`) VALUES (@DomainEventId,@Payload)";

            var maxNumErrorTries = 3;
            var maxExecutionTime = TimeSpan.FromSeconds(3);
            var expectedRows     = 2;

            bool ErrorFilter(Exception exc, int attempt)
            {
                if (exc is MySqlException inner &&
                    inner.HasDuplicateEntry())
                {
                    _logger.LogWarning($"{domainEventRecord.AggregateRootType}: [Ignored]find duplicated domain event from mysql state backend:{inner.Message}.");

                    return(false);
                }

                _logger.LogError($"Append domain event has unknown exception: {LogFormatter.PrintException(exc)}.");

                return(true);
            }

            var affectedRows = await AsyncExecutorWithRetries.ExecuteWithRetriesAsync(async attempt =>
            {
                return(await _db.ExecuteAsync(
                           $"{insertDomainEventIndexSql};{insertDomainEventSql};",
                           command => command.Parameters.AddRange(parameters),
                           token));
            }, maxNumErrorTries, ErrorFilter, maxExecutionTime).ConfigureAwait(false);

            if (affectedRows != expectedRows)
            {
                return(DomainEventResult.StorageFailed(@event.Id,
                                                       $"The affected rows returned MySql state backend is incorrect, expected: {expectedRows}, actual: {affectedRows}."));
            }

            return(DomainEventResult.StorageSucceed(@event.Id));
        }
コード例 #3
0
ファイル: AggregateRoot.cs プロジェクト: caiokf/smart-track
        public virtual void Apply(IDomainEvent e)
        {
            var eventType = e.GetType();
            var castedEvent = Convert.ChangeType(e, eventType);

            var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

            var methods = GetType().GetMethods(bindingFlags)
                .Where(x => x.GetParameters().Count() > 0)
                .Where(x => x.Name.Contains("Apply"))
                .Where(x => x.GetParameters()[0].ParameterType == eventType)
                .ToList();

            if (methods.Count() <= 0)
                throw new ArgumentException(string.Format("Event '{0}' cannot be processed by this aggregate", e.GetType()));

            if (e.IsValid())
                methods.First().Invoke(this, bindingFlags, null, new[] {castedEvent}, null);
        }