Esempio n. 1
0
            private void Handle(IAcSession acSession, Guid infoDicId, bool isCommand)
            {
                var          acDomain          = _set._acDomain;
                var          infoDicDicById    = _set._infoDicDicById;
                var          infoDicDicByCode  = _set._infoDicDicByCode;
                var          infoDicRepository = acDomain.RetrieveRequiredService <IRepository <InfoDic, Guid> >();
                InfoDicState infoDic;

                if (!acDomain.NodeHost.InfoDics.TryGetInfoDic(infoDicId, out infoDic))
                {
                    return;
                }
                var infoDicItems = acDomain.NodeHost.InfoDics.GetInfoDicItems(infoDic);

                if (infoDicItems != null && infoDicItems.Count > 0)
                {
                    throw new ValidationException("信息字典下有信息字典项时不能删除");
                }
                InfoDic entity = infoDicRepository.GetByKey(infoDicId);

                if (entity == null)
                {
                    return;
                }
                var bkState = InfoDicState.Create(acDomain, entity);

                lock (Locker)
                {
                    if (infoDicDicById.ContainsKey(entity.Id))
                    {
                        infoDicDicById.Remove(entity.Id);
                    }
                    if (infoDicDicByCode.ContainsKey(entity.Code))
                    {
                        infoDicDicByCode.Remove(entity.Code);
                    }
                    if (isCommand)
                    {
                        try
                        {
                            infoDicRepository.Remove(entity);
                            infoDicRepository.Context.Commit();
                        }
                        catch
                        {
                            if (!infoDicDicById.ContainsKey(entity.Id))
                            {
                                infoDicDicById.Add(entity.Id, bkState);
                            }
                            if (!infoDicDicByCode.ContainsKey(entity.Code))
                            {
                                infoDicDicByCode.Add(entity.Code, bkState);
                            }
                            infoDicRepository.Context.Rollback();
                            throw;
                        }
                    }
                }
                if (isCommand)
                {
                    acDomain.MessageDispatcher.DispatchMessage(new InfoDicRemovedEvent(acSession, entity, isPrivate: true));
                }
            }
Esempio n. 2
0
            private void Handle(IAcSession acSession, IInfoDicCreateIo input, bool isCommand)
            {
                var acDomain          = _set._acDomain;
                var infoDicDicById    = _set._infoDicDicById;
                var infoDicDicByCode  = _set._infoDicDicByCode;
                var infoDicRepository = acDomain.RetrieveRequiredService <IRepository <InfoDic, Guid> >();

                if (string.IsNullOrEmpty(input.Code))
                {
                    throw new ValidationException("编码不能为空");
                }
                if (!input.Id.HasValue)
                {
                    throw new ValidationException("标识是必须的");
                }
                InfoDic entity;

                lock (Locker)
                {
                    InfoDicState infoDic;
                    if (acDomain.NodeHost.InfoDics.TryGetInfoDic(input.Id.Value, out infoDic))
                    {
                        throw new ValidationException("给定标识标识的记录已经存在");
                    }
                    if (acDomain.NodeHost.InfoDics.TryGetInfoDic(input.Code, out infoDic) && infoDic.Id != input.Id.Value)
                    {
                        throw new ValidationException("重复的编码");
                    }

                    entity = InfoDic.Create(input);

                    var state = InfoDicState.Create(acDomain, entity);
                    if (!infoDicDicById.ContainsKey(entity.Id))
                    {
                        infoDicDicById.Add(entity.Id, state);
                    }
                    if (!infoDicDicByCode.ContainsKey(entity.Code))
                    {
                        infoDicDicByCode.Add(entity.Code, state);
                    }
                    if (isCommand)
                    {
                        try
                        {
                            infoDicRepository.Add(entity);
                            infoDicRepository.Context.Commit();
                        }
                        catch
                        {
                            if (infoDicDicById.ContainsKey(entity.Id))
                            {
                                infoDicDicById.Remove(entity.Id);
                            }
                            if (infoDicDicByCode.ContainsKey(entity.Code))
                            {
                                infoDicDicByCode.Remove(entity.Code);
                            }
                            infoDicRepository.Context.Rollback();
                            throw;
                        }
                    }
                }
                if (isCommand)
                {
                    acDomain.MessageDispatcher.DispatchMessage(new InfoDicAddedEvent(acSession, entity, input, isPrivate: true));
                }
            }