Пример #1
0
        protected IEventCacheReadObject CreateOrJoinSimpleEvent(
            Guid componentId,
            Event eventObj,
            Guid?oldEventId,
            TimeSpan joinInterval,
            Guid accountId,
            out bool isEventNew)
        {
            IEventCacheReadObject rLastEvent = null;

            if (oldEventId.HasValue)
            {
                rLastEvent = AllCaches.Events.Find(new AccountCacheRequest()
                {
                    AccountId = accountId,
                    ObjectId  = oldEventId.Value
                });
            }
            else
            {
                var accountDbContext = Context.DbContext.GetAccountDbContext(accountId);
                var repository       = accountDbContext.GetEventRepository();
                var lastEvent        = repository.GetForJoin(eventObj);
                if (lastEvent != null)
                {
                    rLastEvent = AllCaches.Events.GetForRead(lastEvent, accountId);
                }
            }

            if (rLastEvent != null)
            {
                var request = new AccountCacheRequest()
                {
                    AccountId = accountId,
                    ObjectId  = rLastEvent.Id
                };
                if (AllCaches.Events.ExistsInStorage(request))
                {
                    using (var wLastEvent = AllCaches.Events.Write(request))
                    {
                        bool canJoin = EventHelper.CanJoinSimpleEvents(wLastEvent, eventObj, joinInterval);
                        if (canJoin)
                        {
                            // Можно склеивать
                            UpdateJoinedEvent(accountId, componentId, wLastEvent, eventObj);
                            isEventNew = false;
                            wLastEvent.BeginSave();
                            return(wLastEvent);
                        }
                    }
                }
            }


            // Создадим новое
            {
                var accountDbContext = Context.DbContext.GetAccountDbContext(accountId);
                var eventRepository  = accountDbContext.GetEventRepository();
                isEventNew = true;
                eventRepository.Add(eventObj);
                accountDbContext.SaveChanges();
                var wEvent = EventCacheWriteObject.CreateForUpdate(eventObj, accountId);
                AllCaches.Events.AddOrGet(wEvent);
                return(wEvent);
            }
        }