private void Init() { if (_initialized) { return; } lock (Locker) { if (_initialized) { return; } _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitingEvent(this)); _dicByCode.Clear(); _dicById.Clear(); var functions = _acDomain.RetrieveRequiredService <IOriginalHostStateReader>().GetAllFunctions(); foreach (var entity in functions) { var function = FunctionState.Create(_acDomain, entity); _dicById.Add(function.Id, function); if (!_dicByCode.ContainsKey(function.Resource)) { _dicByCode.Add(function.Resource, new Dictionary <functionCode, FunctionState>(StringComparer.OrdinalIgnoreCase)); } if (!_dicByCode[function.Resource].ContainsKey(function.Code)) { _dicByCode[function.Resource].Add(function.Code, function); } } _initialized = true; _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitializedEvent(this)); } }
private void Handle(IAcSession acSession, IFunctionUpdateIo input, bool isCommand) { var acDomain = _set._acDomain; var functionRepository = acDomain.RetrieveRequiredService <IRepository <Function> >(); if (string.IsNullOrEmpty(input.Code)) { throw new ValidationException("编码不能为空"); } FunctionState bkState; if (!acDomain.FunctionSet.TryGetFunction(input.Id, out bkState)) { throw new NotExistException(); } CatalogState resource; if (!acDomain.CatalogSet.TryGetCatalog(bkState.ResourceTypeId, out resource)) { throw new ValidationException("意外的功能资源标识" + bkState.ResourceTypeId); } Function entity; bool stateChanged = false; lock (Locker) { FunctionState oldState; if (!acDomain.FunctionSet.TryGetFunction(input.Id, out oldState)) { throw new NotExistException(); } FunctionState functionState; if (acDomain.FunctionSet.TryGetFunction(resource, input.Code, out functionState) && functionState.Id != input.Id) { throw new ValidationException("重复的编码"); } entity = functionRepository.GetByKey(input.Id); if (entity == null) { throw new NotExistException("更新的实体不存在"); } entity.Update(input); var newState = FunctionState.Create(acDomain, entity); stateChanged = newState != bkState; if (stateChanged) { Update(newState); } if (isCommand) { try { functionRepository.Update(entity); functionRepository.Context.Commit(); } catch { if (stateChanged) { Update(bkState); } functionRepository.Context.Rollback(); throw; } } } if (isCommand && stateChanged) { acDomain.MessageDispatcher.DispatchMessage(new FunctionUpdatedEvent(acSession, entity, input) { IsPriviate = true }); } }
private void Handle(IAcSession acSession, IFunctionCreateIo input, bool isCommand) { var acDomain = _set._acDomain; var dicByCode = _set._dicByCode; var dicById = _set._dicById; var functionRepository = acDomain.RetrieveRequiredService <IRepository <Function> >(); if (string.IsNullOrEmpty(input.Code)) { throw new ValidationException("编码不能为空"); } if (!input.Id.HasValue) { throw new ValidationException("标识是必须的"); } CatalogState resource; if (!acDomain.CatalogSet.TryGetCatalog(input.ResourceTypeId, out resource)) { throw new ValidationException("意外的功能资源标识" + input.ResourceTypeId); } var entity = Function.Create(input); lock (Locker) { FunctionState functionState; if (acDomain.FunctionSet.TryGetFunction(input.Id.Value, out functionState)) { throw new AnycmdException("记录已经存在"); } var state = FunctionState.Create(acDomain, entity); if (acDomain.FunctionSet.TryGetFunction(resource, input.Code, out functionState)) { throw new ValidationException("重复的编码"); } if (!dicById.ContainsKey(entity.Id)) { dicById.Add(entity.Id, state); } if (!dicByCode.ContainsKey(resource)) { dicByCode.Add(resource, new Dictionary <functionCode, FunctionState>(StringComparer.OrdinalIgnoreCase)); } if (!dicByCode[resource].ContainsKey(entity.Code)) { dicByCode[resource].Add(state.Code, state); } if (isCommand) { try { functionRepository.Add(entity); functionRepository.Context.Commit(); } catch { if (dicById.ContainsKey(entity.Id)) { dicById.Remove(entity.Id); } if (dicByCode.ContainsKey(resource) && dicByCode[resource].ContainsKey(entity.Code)) { dicByCode[resource].Remove(entity.Code); } functionRepository.Context.Rollback(); throw; } } } if (isCommand) { acDomain.MessageDispatcher.DispatchMessage(new FunctionAddedEvent(acSession, entity, input) { IsPriviate = true }); } }