private void Init() { if (_initialized) { return; } lock (Locker) { if (_initialized) { return; } _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitingEvent(this)); _dicById.Clear(); _dicByCode.Clear(); var buttons = _acDomain.RetrieveRequiredService <IOriginalHostStateReader>().GetAllButtons().ToList(); foreach (var button in buttons) { if (_dicById.ContainsKey(button.Id)) { throw new AnycmdException("意外重复的按钮标识"); } if (_dicByCode.ContainsKey(button.Code)) { throw new AnycmdException("意外重复的按钮编码"); } var buttonState = ButtonState.Create(button); _dicById.Add(button.Id, buttonState); _dicByCode.Add(button.Code, buttonState); } _initialized = true; _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitializedEvent(this)); } }
private void Handle(IAcSession acSession, IButtonUpdateIo input, bool isCommand) { var acDomain = _set._acDomain; var buttonRepository = acDomain.RetrieveRequiredService <IRepository <Button> >(); if (string.IsNullOrEmpty(input.Code)) { throw new ValidationException("编码不能为空"); } ButtonState bkState; if (!acDomain.ButtonSet.TryGetButton(input.Id, out bkState)) { throw new NotExistException("意外的按钮标识" + input.Id); } Button entity; var stateChanged = false; lock (MessageLocker) { ButtonState oldState; if (!acDomain.ButtonSet.TryGetButton(input.Id, out oldState)) { throw new NotExistException("意外的按钮标识" + input.Id); } ButtonState button; if (acDomain.ButtonSet.TryGetButton(input.Code, out button) && button.Id != input.Id) { throw new ValidationException("重复的按钮编码"); } entity = buttonRepository.GetByKey(input.Id); if (entity == null) { throw new NotExistException(); } entity.Update(input); var newState = ButtonState.Create(entity); stateChanged = bkState != newState; if (stateChanged) { Update(newState); } if (isCommand) { try { buttonRepository.Update(entity); buttonRepository.Context.Commit(); } catch { if (stateChanged) { Update(bkState); } buttonRepository.Context.Rollback(); throw; } } } if (isCommand && stateChanged) { acDomain.MessageDispatcher.DispatchMessage(new ButtonUpdatedEvent(acSession, entity, input, isPrivate: true)); } }
private void Handle(IAcSession acSession, IButtonCreateIo input, bool isCommand) { var acDomain = _set._acDomain; var dicById = _set._dicById; var dicByCode = _set._dicByCode; var buttonRepository = acDomain.RetrieveRequiredService <IRepository <Button> >(); if (!input.Id.HasValue) { throw new AnycmdException("标识是必须的"); } if (string.IsNullOrEmpty(input.Code)) { throw new ValidationException("编码不能为空"); } Button entity; lock (MessageLocker) { if (acDomain.ButtonSet.ContainsButton(input.Id.Value)) { throw new AnycmdException("给定标识的记录已经存在" + input.Id); } if (acDomain.ButtonSet.ContainsButton(input.Code)) { throw new ValidationException("重复的按钮编码"); } entity = Button.Create(input); var buttonState = ButtonState.Create(entity); if (!dicById.ContainsKey(entity.Id)) { dicById.Add(entity.Id, buttonState); } if (!dicByCode.ContainsKey(entity.Code)) { dicByCode.Add(entity.Code, buttonState); } if (isCommand) { try { buttonRepository.Add(entity); buttonRepository.Context.Commit(); } catch { if (dicById.ContainsKey(entity.Id)) { dicById.Remove(entity.Id); } if (dicByCode.ContainsKey(entity.Code)) { dicByCode.Remove(entity.Code); } buttonRepository.Context.Rollback(); throw; } } } if (isCommand) { acDomain.MessageDispatcher.DispatchMessage(new ButtonAddedEvent(acSession, entity, input, isPrivate: true)); } }