示例#1
0
        public async Task SaveEventsAsync(IReadOnlyCollection <Event <string, string> > events)
        {
            var newEvents = new LinkedList <(Event StorageEvent, Event <string, string> DomainEvent)>();

            foreach (var @event in events)
            {
                var eventInfo = GetEventInfo(@event);

                if (@event.Id.HasValue)
                {
                    var trackedEvent = _db.ChangeTracker.Entries <Event>().FirstOrDefault(e => e.Entity.Id == @event.Id.Value);
                    if (trackedEvent != null)
                    {
                        trackedEvent.State = EntityState.Detached;
                    }

                    eventInfo.Id = @event.Id.Value;
                    _db.Entry(eventInfo).State = EntityState.Modified;
                }
                else
                {
                    newEvents.AddLast((eventInfo, @event));
                    _db.Entry(eventInfo).State = EntityState.Added;
                }
            }

            await _db.SaveChangesAsync();

            foreach (var corresondence in newEvents)
            {
                corresondence.DomainEvent.Id = corresondence.StorageEvent.Id;
            }
        }
示例#2
0
        private async Task ResetPlaces(Hierarchy <string> places)
        {
            var oldPlacesMap = await _db.Places.ToDictionaryAsync(p => p.Id, p => p);

            foreach (var place in places)
            {
                if (oldPlacesMap.ContainsKey(place.Id))
                {
                    var oldPlace = oldPlacesMap[place.Id];
                    oldPlace.Content = place.Content;
                    oldPlace.Left    = place.Left;
                    oldPlace.Right   = place.Right;

                    _db.Entry(oldPlace).State = EntityState.Modified;

                    oldPlacesMap.Remove(place.Id);
                }
                else
                {
                    var newPlace = new Place
                    {
                        Id      = place.Id,
                        Content = place.Content,
                        Left    = place.Left,
                        Right   = place.Right
                    };

                    _db.Places.Add(newPlace);
                }
            }

            foreach (var place in oldPlacesMap.Values)
            {
                _db.Places.Remove(place);
            }

            await _db.SaveChangesAsync();
        }
        public async Task SaveEventsSetsAsync(params EventsSet <string, string>[] eventsSets)
        {
            if (eventsSets is null)
            {
                throw new ArgumentNullException(nameof(eventsSets));
            }

            foreach (var eventsSet in eventsSets)
            {
                if (eventsSet is null)
                {
                    throw new ArgumentException("Events sets should not be null", nameof(eventsSets));
                }
                // Save all events in the set

                var eventsRepo = new EventsRepository(_db);

                await eventsRepo.SaveEventsAsync(eventsSet.Events);

                // Save events set

                EventsSet eventsSetForStorage;

                if (eventsSet.Id.HasValue)
                {
                    var trackedEventsSet = _db.ChangeTracker.Entries <EventsSet>().FirstOrDefault(e => e.Entity.Id == eventsSet.Id.Value);
                    if (trackedEventsSet != null)
                    {
                        trackedEventsSet.State = EntityState.Detached;
                    }

                    eventsSetForStorage = new EventsSet
                    {
                        Id   = eventsSet.Id.Value,
                        Name = eventsSet.Name
                    };
                    _db.Entry(eventsSetForStorage).State = EntityState.Modified;
                }
                else
                {
                    eventsSetForStorage = new EventsSet
                    {
                        Name = eventsSet.Name
                    };
                    _db.Entry(eventsSetForStorage).State = EntityState.Added;
                }

                await _db.SaveChangesAsync();

                eventsSet.Id = eventsSetForStorage.Id;

                // Save events correspondence

                _db.RemoveRange(_db.EventsInSets.Where(i => i.SetId == eventsSetForStorage.Id));

                foreach (var @event in eventsSet.Events)
                {
                    _db.EventsInSets.Add(new EventInSet
                    {
                        SetId   = eventsSetForStorage.Id,
                        EventId = @event.Id.Value
                    });
                }

                await _db.SaveChangesAsync();
            }
        }