Пример #1
0
        public async Task SaveEventsAsync(core.Shared.Aggregate agg, string entity, Guid userId)
        {
            var db = _redis.GetDatabase();

            int version = agg.Version;

            var eventsToBlast = new List <AggregateEvent>();

            foreach (var e in agg.Events.Skip(agg.Version))
            {
                var se = new storage.shared.StoredAggregateEvent
                {
                    Entity  = entity,
                    Event   = e,
                    Key     = e.Id.ToString(),
                    UserId  = userId,
                    Created = e.When,
                    Version = ++version
                };

                var globalKey  = $"{entity}:{userId}";
                var entityKey  = $"{entity}:{userId}:{e.Id}";
                var keyToStore = $"{entity}:{userId}:{e.Id}:{version}";

                var fields = new HashEntry[] {
                    new HashEntry("created", e.When.ToString("o")),
                    new HashEntry("entity", entity),
                    new HashEntry("event", se.EventJson),
                    new HashEntry("key", e.Id.ToString()),
                    new HashEntry("userId", userId.ToString()),
                    new HashEntry("version", version),
                };

                await db.SetAddAsync(globalKey, keyToStore);

                await db.SetAddAsync(entityKey, keyToStore);

                await db.HashSetAsync(keyToStore, fields);

                eventsToBlast.Add(e);
            }

            foreach (var e in eventsToBlast)
            {
                if (e is INotification n)
                {
                    await _mediator.Publish(n);
                }
            }
        }
    public async Task SaveEventsAsync(Aggregate agg, string entity, Guid userId)
    {
        var key = MakeKey(entity, userId);

        if (!_aggregates.ContainsKey(key))
        {
            _aggregates[key] = new List <StoredAggregateEvent>();
        }

        var version = agg.Version;

        var eventsToBlast = new List <AggregateEvent>();

        foreach (var e in agg.Events.Skip(agg.Version))
        {
            var se = new storage.shared.StoredAggregateEvent
            {
                Entity  = entity,
                Event   = e,
                Key     = e.Id.ToString(),
                UserId  = userId,
                Created = e.When,
                Version = ++version
            };

            _aggregates[key].Add(se);
            eventsToBlast.Add(e);
        }

        foreach (var e in eventsToBlast)
        {
            if (e is INotification n)
            {
                await _mediator.Publish(n);
            }
        }

        if (eventsToBlast.Count > 0)
        {
            await _mediator.Publish(new ScheduleUserChanged(userId));
        }
    }