private void Init() { if (_initialized) { return; } lock (Locker) { if (_initialized) { return; } _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitingEvent(this)); _dicByCode.Clear(); _dicById.Clear(); var appSystems = _acDomain.RetrieveRequiredService <IOriginalHostStateReader>().GetAllAppSystems(); foreach (var appSystem in appSystems) { Debug.Assert(appSystem != null, "appSystem != null"); if (_dicByCode.ContainsKey(appSystem.Code)) { throw new AnycmdException("意外重复的应用系统编码" + appSystem.Code); } if (_dicById.ContainsKey(appSystem.Id)) { throw new AnycmdException("意外重复的应用系统标识" + appSystem.Id); } var value = AppSystemState.Create(_acDomain, appSystem); _dicByCode.Add(appSystem.Code, value); _dicById.Add(appSystem.Id, value); } _initialized = true; _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitializedEvent(this)); } }
public void StateObjectEquals() { var acDomain = TestHelper.GetAcDomain(); var entity = new AppSystem { Id = Guid.NewGuid(), Name = "app1", Code = "app1", Description = string.Empty, Icon = string.Empty, IsEnabled = 1, PrincipalId = acDomain.SysUserSet.GetDevAccounts().First().Id, SortCode = 10, SsoAuthAddress = string.Empty, ImageUrl = string.Empty }; var appSystem1 = AppSystemState.Create(acDomain, entity); var appSystem2 = appSystem1; Assert.AreEqual(appSystem1, appSystem1); Assert.IsTrue(appSystem1 == appSystem2); Assert.IsTrue(appSystem1.Equals(appSystem2)); entity = new AppSystem { Id = appSystem1.Id, Name = "app1", Code = "app1", Description = string.Empty, Icon = string.Empty, IsEnabled = 1, PrincipalId = appSystem1.PrincipalId, SortCode = 10, SsoAuthAddress = string.Empty, ImageUrl = string.Empty }; appSystem2 = AppSystemState.Create(acDomain, entity); Assert.AreEqual(appSystem1, appSystem1); Assert.IsTrue(appSystem1 == appSystem2); Assert.IsTrue(appSystem1.Equals(appSystem2)); entity.Code = "app"; appSystem2 = AppSystemState.Create(acDomain, entity); Assert.AreNotEqual(appSystem1, appSystem2); Assert.IsFalse(appSystem1 == appSystem2); Assert.IsFalse(appSystem1.Equals(appSystem2)); }
private void Handle(IAcSession acSession, IAppSystemUpdateIo input, bool isCommand) { var acDomain = _set._acDomain; var repository = acDomain.RetrieveRequiredService <IRepository <AppSystem> >(); if (string.IsNullOrEmpty(input.Code)) { throw new ValidationException("编码不能为空"); } AppSystemState bkState; if (!acDomain.AppSystemSet.TryGetAppSystem(input.Id, out bkState)) { throw new NotExistException("意外的应用系统标识" + input.Id); } AppSystem entity; var stateChanged = false; lock (MessageLocker) { AppSystemState oldState; if (!acDomain.AppSystemSet.TryGetAppSystem(input.Id, out oldState)) { throw new NotExistException("意外的应用系统标识" + input.Id); } AppSystemState outAppSystem; if (acDomain.AppSystemSet.TryGetAppSystem(input.Code, out outAppSystem) && outAppSystem.Id != input.Id) { throw new ValidationException("重复的应用系统编码" + input.Code); } entity = repository.GetByKey(input.Id); if (entity == null) { throw new NotExistException(); } entity.Update(input); var newState = AppSystemState.Create(acDomain, entity); stateChanged = newState != bkState; if (stateChanged) { Update(newState); } if (isCommand) { try { repository.Update(entity); repository.Context.Commit(); } catch { if (stateChanged) { Update(bkState); } repository.Context.Rollback(); throw; } } } if (isCommand && stateChanged) { acDomain.MessageDispatcher.DispatchMessage(new AppSystemUpdatedEvent(acSession, entity, input, isPrivate: true)); } }
private void Handle(IAcSession acSession, IAppSystemCreateIo input, bool isCommand) { var dicByCode = _set._dicByCode; var dicById = _set._dicById; var acDomain = _set._acDomain; var repository = acDomain.RetrieveRequiredService <IRepository <AppSystem> >(); if (string.IsNullOrEmpty(input.Code)) { throw new ValidationException("编码不能为空"); } if (!input.Id.HasValue) { throw new AnycmdException("标识是必须的"); } AppSystem entity; lock (MessageLocker) { if (acDomain.AppSystemSet.ContainsAppSystem(input.Id.Value)) { throw new AnycmdException("给定标识的记录已经存在" + input.Id); } if (acDomain.AppSystemSet.ContainsAppSystem(input.Code)) { throw new ValidationException("重复的应用系统编码" + input.Code); } AccountState principal; // TODO:考虑将AppSystem.PrincipalId重命名为AppSystem.DevPrincipalId,从而与业务负责人分开。 if (!acDomain.SysUserSet.TryGetDevAccount(input.PrincipalId, out principal)) { throw new ValidationException("意外的应用系统负责人,业务系统负责人必须是开发人员"); } entity = AppSystem.Create(input); var state = AppSystemState.Create(acDomain, entity); if (!dicByCode.ContainsKey(state.Code)) { dicByCode.Add(state.Code, state); } if (!dicById.ContainsKey(state.Id)) { dicById.Add(state.Id, state); } // 如果是命令则持久化 if (isCommand) { try { repository.Add(entity); repository.Context.Commit(); } catch { if (dicByCode.ContainsKey(entity.Code)) { dicByCode.Remove(entity.Code); } if (dicById.ContainsKey(entity.Id)) { dicById.Remove(entity.Id); } repository.Context.Rollback(); throw; } } } // 如果是命令则分发事件 if (isCommand) { acDomain.MessageDispatcher.DispatchMessage(new AppSystemAddedEvent(acSession, entity, input, isPrivate: true)); } }