private void Init() { if (_initialized) { return; } lock (Locker) { if (_initialized) { return; } _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitingEvent(this)); _groupDic.Clear(); var groups = _acDomain.RetrieveRequiredService <IOriginalHostStateReader>().GetAllGroups(); foreach (var group in groups) { if (!_groupDic.ContainsKey(@group.Id)) { _groupDic.Add(@group.Id, GroupState.Create(@group)); } } _initialized = true; _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitializedEvent(this)); } }
private void Handle(IAcSession acSession, IGroupCreateIo input, bool isCommand) { var acDomain = _set._acDomain; var groupDic = _set._groupDic; var groupRepository = acDomain.RetrieveRequiredService <IRepository <Group, Guid> >(); if (!input.Id.HasValue) { throw new ValidationException("标识是必须的"); } var entity = Group.Create(input); lock (Locker) { GroupState group; if (acDomain.GroupSet.TryGetGroup(entity.Id, out group)) { throw new GeneralException("意外的重复标识"); } if (acDomain.GroupSet.Any(a => a.Name.Equals(input.Name, StringComparison.OrdinalIgnoreCase))) { throw new ValidationException("重复的工作组名"); } if (!groupDic.ContainsKey(entity.Id)) { groupDic.Add(entity.Id, GroupState.Create(entity)); } if (isCommand) { try { groupRepository.Add(entity); groupRepository.Context.Commit(); } catch { if (groupDic.ContainsKey(entity.Id)) { groupDic.Remove(entity.Id); } groupRepository.Context.Rollback(); throw; } } } if (isCommand) { acDomain.MessageDispatcher.DispatchMessage(new GroupAddedEvent(acSession, entity, input, isPrivate: true)); } }
private void Handle(IAcSession acSession, IGroupUpdateIo input, bool isCommand) { var acDomain = _set._acDomain; var groupRepository = acDomain.RetrieveRequiredService <IRepository <Group, Guid> >(); GroupState bkState; if (!acDomain.GroupSet.TryGetGroup(input.Id, out bkState)) { throw new NotExistException(); } Group entity; var stateChanged = false; lock (Locker) { GroupState oldState; if (!acDomain.GroupSet.TryGetGroup(input.Id, out oldState)) { throw new NotExistException(); } if (acDomain.GroupSet.Any(a => a.Name.Equals(input.Name, StringComparison.OrdinalIgnoreCase) && a.Id != input.Id)) { throw new ValidationException("重复的工作组名"); } entity = groupRepository.GetByKey(input.Id); if (entity == null) { throw new NotExistException(); } entity.Update(input); var newState = GroupState.Create(entity); stateChanged = newState != bkState; if (stateChanged) { Update(newState); } if (isCommand) { try { groupRepository.Update(entity); groupRepository.Context.Commit(); } catch { if (stateChanged) { Update(bkState); } groupRepository.Context.Rollback(); throw; } } } if (isCommand && stateChanged) { acDomain.MessageDispatcher.DispatchMessage(new GroupUpdatedEvent(acSession, entity, input, isPrivate: true)); } }