private void Init() { if (_initialized) { return; } lock (Locker) { if (_initialized) { return; } _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitingEvent(this)); _dicByCode.Clear(); _dicById.Clear(); _dicByCode.Add(CatalogState.VirtualRoot.Code, CatalogState.VirtualRoot); _dicById.Add(CatalogState.VirtualRoot.Id, CatalogState.VirtualRoot); var allCatalogs = _acDomain.RetrieveRequiredService <IOriginalHostStateReader>().GetCatalogs().OrderBy(a => a.ParentCode); foreach (var catalog in allCatalogs) { CatalogState orgState = CatalogState.Create(_acDomain, catalog); if (!_dicByCode.ContainsKey(catalog.Code)) { _dicByCode.Add(catalog.Code, orgState); } if (!_dicById.ContainsKey(catalog.Id)) { _dicById.Add(catalog.Id, orgState); } } _initialized = true; _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitializedEvent(this)); } }
private void Handle(IAcSession acSession, ICatalogUpdateIo input, bool isCommand) { var acDomain = _set._acDomain; var catalogRepository = acDomain.RetrieveRequiredService <IRepository <Catalog> >(); if (string.IsNullOrEmpty(input.Code)) { throw new ValidationException("编码不能为空"); } CatalogState bkState; if (!acDomain.CatalogSet.TryGetCatalog(input.Id, out bkState)) { throw new ValidationException("给定标识的目录不存在" + input.Id); } Catalog entity; var stateChanged = false; lock (MessageLocker) { CatalogState oragnization; if (acDomain.CatalogSet.TryGetCatalog(input.Code, out oragnization) && oragnization.Id != input.Id) { throw new ValidationException("重复的目录码" + input.Code); } if (!string.IsNullOrEmpty(input.ParentCode)) { CatalogState parentOragnization; if (!acDomain.CatalogSet.TryGetCatalog(input.ParentCode, out parentOragnization)) { throw new ValidationException("标识为" + input.ParentCode + "的父目录不存在"); } if (input.ParentCode.Equals(input.Code, StringComparison.OrdinalIgnoreCase)) { throw new AnycmdException("目录的父目录不能是自己"); } if (!input.Code.StartsWith(parentOragnization.Code, StringComparison.OrdinalIgnoreCase)) { throw new ValidationException("子级目录的编码必须以父级目录编码为前缀"); } if (input.ParentCode.StartsWith(input.Code, StringComparison.OrdinalIgnoreCase)) { throw new AnycmdException("目录的父目录不能是自己的子孙级目录"); } } entity = catalogRepository.GetByKey(input.Id); if (entity == null) { throw new NotExistException(); } entity.Update(input); var newState = CatalogState.Create(acDomain, entity); stateChanged = newState != bkState; if (stateChanged) { Update(newState); } if (isCommand) { try { catalogRepository.Update(entity); catalogRepository.Context.Commit(); } catch { if (stateChanged) { Update(bkState); } catalogRepository.Context.Rollback(); throw; } } } if (isCommand && stateChanged) { acDomain.MessageDispatcher.DispatchMessage(new CatalogUpdatedEvent(acSession, entity, input, isPrivate: true)); } }
private void Handle(IAcSession acSession, ICatalogCreateIo input, bool isCommand) { var acDomain = _set._acDomain; var dicByCode = _set._dicByCode; var dicById = _set._dicById; var catalogRepository = acDomain.RetrieveRequiredService <IRepository <Catalog> >(); if (!input.Id.HasValue) { throw new ValidationException("标识是必须的"); } if (string.IsNullOrEmpty(input.Code)) { throw new ValidationException("编码不能为空"); } if (string.IsNullOrEmpty(input.Name)) { throw new ValidationException("名称是必须的"); } Catalog entity; lock (MessageLocker) { CatalogState catalog; if (acDomain.CatalogSet.TryGetCatalog(input.Id.Value, out catalog)) { throw new ValidationException("给定标识的目录已经存在"); } if (acDomain.CatalogSet.TryGetCatalog(input.Code, out catalog)) { throw new ValidationException("重复的目录码"); } if (!string.IsNullOrEmpty(input.ParentCode)) { CatalogState parentOragnization; if (!acDomain.CatalogSet.TryGetCatalog(input.ParentCode, out parentOragnization)) { throw new ValidationException("标识为" + input.ParentCode + "的父目录不存在"); } if (string.Equals(input.Code, input.ParentCode) || !input.Code.StartsWith(parentOragnization.Code, StringComparison.OrdinalIgnoreCase)) { throw new ValidationException("子级目录的编码必须以父级目录编码为前缀"); } if (acDomain.CatalogSet.Any(a => string.Equals(a.ParentCode, input.ParentCode) && string.Equals(a.Name, input.Name, StringComparison.OrdinalIgnoreCase))) { throw new ValidationException("兄弟目录间不能重名"); } } else { if (acDomain.CatalogSet.Any(a => string.IsNullOrEmpty(a.ParentCode) && string.Equals(a.Name, input.Name, StringComparison.OrdinalIgnoreCase))) { throw new ValidationException("重复的目录名"); } } entity = Catalog.Create(input); var state = CatalogState.Create(acDomain, entity); if (!dicByCode.ContainsKey(entity.Code)) { dicByCode.Add(entity.Code, state); } if (!dicById.ContainsKey(entity.Id)) { dicById.Add(entity.Id, state); } if (isCommand) { try { catalogRepository.Add(entity); catalogRepository.Context.Commit(); } catch { if (dicByCode.ContainsKey(entity.Code)) { dicByCode.Remove(entity.Code); } if (dicById.ContainsKey(entity.Id)) { dicById.Remove(entity.Id); } catalogRepository.Context.Rollback(); throw; } } } if (isCommand) { acDomain.MessageDispatcher.DispatchMessage(new CatalogAddedEvent(acSession, entity, input, isPrivate: true)); } }