protected override ISpecification <OtherDocumentItemDto> ToSpecification(IWorkItemStrategy workItemStrategy)
        {
            var specification = new Specification <OtherDocumentItemDto>();

            if (!(workItemStrategy is OtherDocumentItemWorkItemStrategy strategy))
            {
                return(specification);
            }

            if (strategy.WithNestedItems)
            {
                if (strategy.NestedItemWorkItemStrategy.WithOneMoreNestedItems)
                {
                    specification.FetchStrategy.Add(w => w.Include(x => x.NestedItemDtos)
                                                    .ThenInclude(x => x.OneMoreNestedItemDtos));
                }
                else
                {
                    specification.FetchStrategy.Add(w => w.Include(x => x.NestedItemDtos));
                }
            }

            if (!strategy.WithDeleted)
            {
                specification.And(OnlyNotDeletedSpecification);
            }

            return(specification);
        }
Beispiel #2
0
        protected override ISpecification <DocumentDto> ToSpecification(IWorkItemStrategy documentWorkItemStrategy)
        {
            var specification = new Specification <DocumentDto>();

            specification.FetchStrategy.Add(w => w.Include(x => x.OtherDocumentDto));
            // add Inlcude all inherited types..

            if (!(documentWorkItemStrategy is DocumentWorkItemStrategy strategy))
            {
                return(specification);
            }

            if (strategy.WithAttachments)
            {
                specification.FetchStrategy.Add(
                    w => w.Include(x => x.AttachmentLinkDtos)
                    .ThenInclude(x => x.AttachmentDto)
                    );
            }

            if (!strategy.WithDeleted)
            {
                specification.And(OnlyNotDeletedSpecification);
            }

            return(specification);
        }
Beispiel #3
0
        public override void Save(OtherDocumentDto entity, IWorkItemStrategy updateWorkItemStrategy = null)
        {
            if (!(updateWorkItemStrategy is OtherDocumentWorkItemStrategy otherDocumentWorkItemStrategy))
            {
                base.Save(entity, updateWorkItemStrategy);
                return;
            }

            var entry = Context.Entry(entity);

            if (entry.IsKeySet)
            {
                if (entry.State == EntityState.Detached)
                {
                    throw new Exception("Попытка сохранить сущность, которая не присоединена к контексту");
                }
                entry.State = EntityState.Modified;
            }
            else
            {
                Context.Add(entry);
            }

            if (!otherDocumentWorkItemStrategy.WithAttachments)
            {
                Context.Entry(entity.DocumentDto.AttachmentLinkDtos).State = EntityState.Detached;
            }

            if (!otherDocumentWorkItemStrategy.WithPayments)
            {
                Context.Entry(entity.OtherDocumentPaymentDtos).State = EntityState.Detached;
            }


            if (!otherDocumentWorkItemStrategy.WithItems)
            {
                Context.Entry(entity.OtherDocumentItemDtos).State = EntityState.Detached;
            }
            if (!otherDocumentWorkItemStrategy.OtherDocumentItemWorkItemStrategy.WithNestedItems)
            {
                foreach (var entityOtherDocumentItemDto in entity.OtherDocumentItemDtos)
                {
                    Context.Entry(entityOtherDocumentItemDto.NestedItemDtos).State = EntityState.Detached;
                }
            }
            else if (!otherDocumentWorkItemStrategy.OtherDocumentItemWorkItemStrategy.NestedItemWorkItemStrategy
                     .WithOneMoreNestedItems)
            {
                foreach (var nestedItemDto in entity.OtherDocumentItemDtos.SelectMany(w => w.NestedItemDtos))
                {
                    Context.Entry(nestedItemDto.OneMoreNestedItemDtos).State = EntityState.Unchanged;
                }
            }

            Context.SaveChanges();
        }
Beispiel #4
0
        public virtual TDomainEntity Get(int id, IWorkItemStrategy workItemStrategy = default(IWorkItemStrategy))
        {
            if (workItemStrategy == null)
            {
                return(Repository.Get(id).Reconstitute());
            }

            return(!workItemStrategy.CacheResult
                ? Repository.Get(id, ToSpecification(workItemStrategy)).Reconstitute()
                : GetCached(id, workItemStrategy).Reconstitute());
        }
Beispiel #5
0
        public override void Save(Document entity, IWorkItemStrategy documentWorkItemStrategy = null)
        {
            switch (entity)
            {
            case OtherDocument otherDocument:
                _otherDocumentService.Save(otherDocument, documentWorkItemStrategy);
                break;
                // another document types...
            }

            throw new ArgumentOutOfRangeException(nameof(entity));
        }
        public override void Save(DocumentDto entity, IWorkItemStrategy updateWorkItemStrategy = null)
        {
            if (!(updateWorkItemStrategy is DocumentWorkItemStrategy documentWorkItemStrategy))
            {
                base.Save(entity);
                return;
            }

            var entry = Context.Entry(entity);


            try
            {
                if (documentWorkItemStrategy.WithAttachments)
                {
                    if (entry.State == EntityState.Detached)
                    {
                        Context.Attach(entity);
                    }
                    foreach (var attachmentLinkDto in entity.AttachmentLinkDtos)
                    {
                        Context.Entry(attachmentLinkDto).State = EntityState.Modified;
                    }

                    //var attachedEntity = DbSetTable.Include(w => w.AttachmentLinkDtos).FirstOrDefault(w => w.Id == entity.Id);
                    //    if (attachedEntity != null)
                    //    {
                    //        Context.Entry(attachedEntity).CurrentValues.SetValues(entity);
                    //        var attachmentLinks = attachedEntity.AttachmentLinkDtos.ToList();
                    //        foreach (var attachmentLink in attachmentLinks)
                    //        {
                    //            var attachment = entity.AttachmentLinkDtos.SingleOrDefault(i => i.Id == attachmentLink.Id);
                    //            if (attachment != null)
                    //                Context.Entry(attachmentLink).CurrentValues.SetValues(attachment);
                    //            else
                    //                Context.Remove(attachmentLink);
                    //        }
                    //        return;
                    //    }
                }
            }
            catch (Exception)
            {
                // ignore and try the default behavior
            }

            // default
            entry.State = EntityState.Modified;
        }
Beispiel #7
0
        public virtual IReadOnlyCollection <TDomainEntity> Get(IWorkItemStrategy workItemStrategy = default(IWorkItemStrategy))
        {
            if (workItemStrategy == null)
            {
                return(Repository.Get().Select(w => w.Reconstitute()).ToList());
            }

            if (!workItemStrategy.CacheResult)
            {
                return(Repository.Get(ToSpecification(workItemStrategy)).Select(w => w.Reconstitute())
                       .ToList());
            }

            throw new NotSupportedException();
        }
Beispiel #8
0
        protected override ISpecification <OtherDocumentPaymentDto> ToSpecification(IWorkItemStrategy workItemStrategy)
        {
            var specification = new Specification <OtherDocumentPaymentDto>();

            if (!(workItemStrategy is OtherDocumentPaymentWorkItemStrategy strategy))
            {
                return(specification);
            }

            if (!strategy.WithDeleted)
            {
                specification.And(OnlyNotDeletedSpecification);
            }

            return(specification);
        }
Beispiel #9
0
        public virtual void Save(TDomainEntity entity, IWorkItemStrategy workItemStrategy = null)
        {
            var existingEntity = Context.Find <TDto>(entity.Id);

            if (existingEntity != null)
            {
                existingEntity.Update(entity);
                Context.Update(existingEntity);
                Repository.Save(existingEntity, workItemStrategy);
            }
            else
            {
                var dto = new TDto();
                dto.Update(entity);
                Context.Add(dto);
                Repository.Save(dto);
            }
        }
Beispiel #10
0
        /// <summary>
        /// Читаем с кэша, если там нет, то кэшируем в redis по ключу
        /// </summary>
        /// <param name="id"></param>
        /// <param name="workItemStrategy"></param>
        /// <returns></returns>
        private TDto GetCached(int id, IWorkItemStrategy workItemStrategy = default(IWorkItemStrategy))
        {
            if (workItemStrategy != null)
            {
                return(Repository.Get(id, ToSpecification(workItemStrategy)));
            }

            var entity = CacheService.Get(id);

            if (entity != null)
            {
                return(entity);
            }

            entity = Repository.Get(id);
            CacheService.Set(entity);

            return(entity);
        }
Beispiel #11
0
        public override void Save(TDto entity, IWorkItemStrategy updateWorkItemStrategy = null)
        {
            var entry = Context.Entry(entity);

            if (entry.IsKeySet)
            {
                if (entry.State == EntityState.Detached)
                {
                    throw new Exception("Попытка сохранить сущность, которая не присоединена к контексту");
                }
                entry.State = EntityState.Modified;
            }
            else
            {
                Context.Add(entry);
            }

            Context.SaveChanges();
        }
Beispiel #12
0
        public override void Save(OtherDocumentItemDto entity, IWorkItemStrategy updateWorkItemStrategy = null)
        {
            if (!(updateWorkItemStrategy is OtherDocumentItemWorkItemStrategy otherDocumentItemWorkItemStrategy))
            {
                base.Save(entity);
                return;
            }

            var entry = Context.Entry(entity);

            try
            {
                if (entry.State == EntityState.Detached)
                {
                    Context.Attach(entity);
                }

                if (otherDocumentItemWorkItemStrategy.WithNestedItems)
                {
                    foreach (var nestedItemDto in entity.NestedItemDtos)
                    {
                        Context.Entry(nestedItemDto).State = EntityState.Modified;

                        if (otherDocumentItemWorkItemStrategy.NestedItemWorkItemStrategy.WithOneMoreNestedItems)
                        {
                            foreach (var oneMoreNestedItemDto in nestedItemDto.OneMoreNestedItemDtos)
                            {
                                Context.Entry(oneMoreNestedItemDto).State = EntityState.Modified;
                            }
                        }
                    }
                    return;
                }
            }
            catch (Exception)
            {
                // ignore and try the default behavior
            }

            // default
            entry.State = EntityState.Modified;
        }
Beispiel #13
0
        /// <summary>
        /// Если стратегии выборки нет, то используем кэширование
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="workItemStrategy"></param>
        /// <returns></returns>
        private IReadOnlyCollection <TDto> GetCached(ICollection <int> ids,
                                                     IWorkItemStrategy workItemStrategy = default(IWorkItemStrategy))
        {
            if (workItemStrategy != null)
            {
                return(Repository.Get(ids, ToSpecification(workItemStrategy)).ToList());
            }

            var entityList = CacheService.Get(ids.ToArray());

            if (entityList?.Count == ids.Count)
            {
                return(entityList.ToList());
            }

            entityList = Repository.Get(ids).ToList();
            CacheService.Set(entityList);

            return(entityList.ToList());
        }
Beispiel #14
0
        public virtual IReadOnlyCollection <TDomainEntity> Get(IEnumerable <int> ids,
                                                               IWorkItemStrategy workItemStrategy = default(IWorkItemStrategy))
        {
            if (workItemStrategy == null)
            {
                return(Repository.Get(ids).Select(w => w.Reconstitute()).ToList());
            }

            var idsList = ids.ToList();

            if (idsList.Count == 0)
            {
                return(new List <TDomainEntity>());
            }

            if (!workItemStrategy.CacheResult)
            {
                return(Repository.Get(idsList, ToSpecification(workItemStrategy)).Select(w => w.Reconstitute())
                       .ToList());
            }

            return(GetCached(idsList, workItemStrategy).Select(w => w.Reconstitute()).ToList());
        }
Beispiel #15
0
 /// <summary>
 /// Сохранить сущность
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="updateWorkItemStrategy"></param>
 public abstract void Save(TDto entity, IWorkItemStrategy updateWorkItemStrategy = null);
Beispiel #16
0
 public override IReadOnlyCollection <Document> Get(IEnumerable <int> ids, IWorkItemStrategy workItemStrategy = null)
 {
     return(base.Get(ids, workItemStrategy ?? new DocumentWorkItemStrategy()));
 }
Beispiel #17
0
 public override IReadOnlyCollection <Document> Get(IWorkItemStrategy workItemStrategy = null)
 {
     return(base.Get(workItemStrategy ?? new DocumentWorkItemStrategy()));
 }
Beispiel #18
0
 public override Document Get(int id, IWorkItemStrategy workItemStrategy = null)
 {
     return(base.Get(id, workItemStrategy ?? new DocumentWorkItemStrategy()));
 }
Beispiel #19
0
 protected abstract TSpecification ToSpecification(IWorkItemStrategy workItemStrategy);