Пример #1
0
        public async Task HandleAsync(FakeCommand message)
        {
            Logger.LogInformation("Handle FakeCommand {ObjectId}", message.ObjectId);

            var tasksToRun = new List <Task>();

            await LockProvider.TryUsingAsync(message.ObjectId, async token =>
            {
                var ex = await Repository.CheckAggregateExists <FakeAggregate>(message.ObjectId);

                FakeAggregate agg;

                if (!ex)
                {
                    agg = FakeAggregate.Factory.Create(message.ObjectId);
                }
                else
                {
                    agg = await Repository.GetByIdAsync <FakeAggregate>(message.ObjectId);
                }

                agg.DoFakeEvent(message.Text);
                var evnts = await Repository.SaveAsyncWithOutPush(agg, CancellationToken);

                foreach (var evnt in evnts)
                {
                    tasksToRun.Add(Bus.PublishAsync(evnt));
                }
                //await Repository.SaveAsync(agg, CancellationToken);
            },
                                             cancellationToken : CancellationToken);

            await Task.WhenAll(tasksToRun);
        }
Пример #2
0
        protected async Task DeleteItemIfAsync(IMessage message, Func <T, bool> predicate)
        {
            async Task DoAsync()
            {
                var lockId = GetLockId(message);
                await LockProvider.TryUsingAsync(lockId,
                                                 async token =>
                {
                    var item = await LoadItemAsync(message).ConfigureAwait(false);

                    if (item.Entity == null)
                    {
                        return;
                    }

                    if (!item.IsNew)
                    {
                        if (predicate(item.Entity))
                        {
                            await DeleteAsync(item.Id,
                                              message,
                                              CreateIdentifierItem).ConfigureAwait(false);
                        }
                    }
                }, cancellationToken : CancellationToken.None).ConfigureAwait(false);
            }

            await LockProvider.TryUsingAsync(GetCacheLockId(), async() => await DoAsync().ConfigureAwait(false), timeUntilExpires : null, cancellationToken : CancellationToken.None).ConfigureAwait(false);
        }
Пример #3
0
        protected async Task LoadAndSaveOnlyExistingAsync(IMessage message, Action <T> setValues = null)
        {
            var lockId = GetLockId(message);

            await LockProvider.TryUsingAsync(lockId,
                                             async token =>
            {
                var item   = await LoadItemAsync(message).ConfigureAwait(false);
                var entity = item.Entity;

                if (item.IsNew)
                {
                    Logger.LogTrace("Skipping new Item");
                }
                else
                {
                    if (entity == null)
                    {
                        Logger.LogTrace("No Valid Entity Loaded");
                    }
                    else
                    {
                        setValues?.Invoke(entity);

                        //  await SaveItemAsync(item, message, state);
                        await SaveItemAsync(item, message).ConfigureAwait(false);
                    }
                }
            }, cancellationToken : CancellationToken.None).ConfigureAwait(false);
        }
Пример #4
0
        private async Task SaveItemAsync(EntityDescriptor <T> item, IMessage domainEvent)
        {
            async Task DoAsync()
            {
                if (Context.IsLiveProcessing)
                {
                    await SaveAsync(item, domainEvent, CreateIdentifierItem(item.Entity)).ConfigureAwait(false);
                }
                else
                {
                    await SaveAsync(item, domainEvent, null).ConfigureAwait(false);
                }

                await Cache.RemoveByPrefixAsync(EntityTypeKey).ConfigureAwait(false);
            }

            await LockProvider.TryUsingAsync(GetCacheLockId(), async() => await DoAsync().ConfigureAwait(false), timeUntilExpires : null, cancellationToken : CancellationToken.None).ConfigureAwait(false);
        }
Пример #5
0
        protected async Task DeleteItemAsync(IMessage message)
        {
            async Task DoAsync()
            {
                var item = await LoadItemAsync(message).ConfigureAwait(false);

                if (item.Entity == null)
                {
                    return;
                }

                if (!item.IsNew)
                {
                    await base.DeleteAsync(item.Id,
                                           message,
                                           CreateIdentifierItem).ConfigureAwait(false);

                    //,state: state);
                }
            }

            await LockProvider.TryUsingAsync(GetCacheLockId(), async() => await DoAsync().ConfigureAwait(false), timeUntilExpires : null, cancellationToken : CancellationToken.None).ConfigureAwait(false);
        }
Пример #6
0
        public async Task HandleAsync(FakeEvent message, AggregateTypeInfo <FakeAggregate> info)
        {
            Logger.LogInformation("Handle FakeEvent {AggregateId}", message.AggregateId);


            var tasksToRun = new List <Task>();

            await LockProvider.TryUsingAsync(message.AggregateId,
                                             async token =>
            {
                var agg = await Repository.GetByIdAsync <FakeAggregate>(message.AggregateId);
                agg.DoFakeEvent2("zweite runde 2");
                //await Repository.SaveAsync(agg, CancellationToken);
                var evnts = await Repository.SaveAsyncWithOutPush(agg, CancellationToken);

                foreach (var evnt in evnts)
                {
                    tasksToRun.Add(Bus.PublishAsync(evnt));
                }
            },
                                             cancellationToken : CancellationToken);

            await Task.WhenAll(tasksToRun);
        }