private void Init() { if (_initialized) { return; } lock (Locker) { if (_initialized) { return; } _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitingEvent(this)); _ssdSetDic.Clear(); _ssdRoleBySet.Clear(); _ssdRoleById.Clear(); var stateReder = _acDomain.RetrieveRequiredService <IOriginalHostStateReader>(); var ssdSets = stateReder.GetAllSsdSets(); foreach (var ssdSet in ssdSets) { if (!_ssdSetDic.ContainsKey(ssdSet.Id)) { _ssdSetDic.Add(ssdSet.Id, SsdSetState.Create(ssdSet)); } } var ssdRoles = stateReder.GetAllSsdRoles(); foreach (var ssdRole in ssdRoles) { SsdSetState ssdSetState; if (_ssdSetDic.TryGetValue(ssdRole.SsdSetId, out ssdSetState)) { var state = SsdRoleState.Create(ssdRole); if (!_ssdRoleById.ContainsKey(ssdRole.Id)) { _ssdRoleById.Add(ssdRole.Id, state); } if (!_ssdRoleBySet.ContainsKey(ssdSetState)) { _ssdRoleBySet.Add(ssdSetState, new List <SsdRoleState>()); } _ssdRoleBySet[ssdSetState].Add(state); } else { // TODO:删除非法的记录 } } _initialized = true; _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitializedEvent(this)); } }
private void Handle(IAcSession acSession, ISsdRoleCreateIo input, bool isCommand) { var acDomain = _set._acDomain; var ssdRoleBySet = _set._ssdRoleBySet; var ssdRoleById = _set._ssdRoleById; var ssdRoleRepository = acDomain.RetrieveRequiredService <IRepository <SsdRole, Guid> >(); if (!input.Id.HasValue) { throw new ValidationException("标识是必须的"); } if (ssdRoleById.Any(a => a.Key == input.Id.Value || (a.Value.RoleId == input.RoleId && a.Value.SsdSetId == input.SsdSetId))) { throw new ValidationException("重复的记录"); } SsdSetState ssdSet; if (!acDomain.SsdSetSet.TryGetSsdSet(input.SsdSetId, out ssdSet)) { throw new ValidationException("意外的静态责任分离角色集标识" + input.SsdSetId); } var entity = SsdRole.Create(input); lock (Locker) { if (ssdRoleById.Any(a => a.Key == input.Id.Value || (a.Value.RoleId == input.RoleId && a.Value.SsdSetId == input.SsdSetId))) { throw new ValidationException("重复的记录"); } if (!acDomain.SsdSetSet.TryGetSsdSet(input.SsdSetId, out ssdSet)) { throw new ValidationException("意外的静态责任分离角色集标识" + input.SsdSetId); } var state = SsdRoleState.Create(entity); if (!ssdRoleById.ContainsKey(entity.Id)) { ssdRoleById.Add(entity.Id, state); } if (!ssdRoleBySet.ContainsKey(ssdSet)) { ssdRoleBySet.Add(ssdSet, new List <SsdRoleState>()); } ssdRoleBySet[ssdSet].Add(state); if (isCommand) { try { ssdRoleRepository.Add(entity); ssdRoleRepository.Context.Commit(); } catch { if (ssdRoleById.ContainsKey(entity.Id)) { ssdRoleById.Remove(entity.Id); } ssdRoleBySet[ssdSet].Remove(state); ssdRoleRepository.Context.Rollback(); throw; } } } if (isCommand) { acDomain.MessageDispatcher.DispatchMessage(new SsdRoleAddedEvent(acSession, entity, input, isPrivate: true)); } }