Ejemplo n.º 1
0
        public bool TryGetDsdSet(Guid dsdSetId, out DsdSetState dsdSet)
        {
            if (!_initialized)
            {
                Init();
            }
            Debug.Assert(dsdSetId != Guid.Empty);

            return(_dsdSetDic.TryGetValue(dsdSetId, out dsdSet));
        }
Ejemplo n.º 2
0
        public IReadOnlyCollection <DsdRoleState> GetDsdRoles(DsdSetState dsdSet)
        {
            if (!_initialized)
            {
                Init();
            }
            if (dsdSet == null)
            {
                throw new ArgumentNullException("dsdSet");
            }

            return(!_dsdRoleBySet.ContainsKey(dsdSet) ? new List <DsdRoleState>() : _dsdRoleBySet[dsdSet]);
        }
Ejemplo n.º 3
0
            private void Update(DsdSetState state)
            {
                var dsdSetDic    = _set._dsdSetDic;
                var dsdRoleBySet = _set._dsdRoleBySet;
                var oldState     = dsdSetDic[state.Id];

                dsdSetDic[state.Id] = state;
                if (dsdRoleBySet.ContainsKey(oldState) && !dsdRoleBySet.ContainsKey(state))
                {
                    dsdRoleBySet.Add(state, dsdRoleBySet[oldState]);
                    dsdRoleBySet.Remove(oldState);
                }
            }
Ejemplo n.º 4
0
            private void Handle(IAcSession acSession, IDsdSetCreateIo input, bool isCommand)
            {
                var acDomain         = _set._acDomain;
                var dsdSetDic        = _set._dsdSetDic;
                var dsdSetRepository = acDomain.RetrieveRequiredService <IRepository <DsdSet> >();

                if (!input.Id.HasValue)
                {
                    throw new ValidationException("标识是必须的");
                }
                if (acDomain.DsdSetSet.Any(a => a.Name.Equals(input.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    throw new ValidationException("重复的动态责任分离角色集名称");
                }

                var entity = DsdSet.Create(input);

                lock (Locker)
                {
                    DsdSetState dsdSet;
                    if (acDomain.DsdSetSet.TryGetDsdSet(entity.Id, out dsdSet))
                    {
                        throw new AnycmdException("意外的重复标识");
                    }
                    if (!dsdSetDic.ContainsKey(entity.Id))
                    {
                        dsdSetDic.Add(entity.Id, DsdSetState.Create(entity));
                    }
                    if (isCommand)
                    {
                        try
                        {
                            dsdSetRepository.Add(entity);
                            dsdSetRepository.Context.Commit();
                        }
                        catch
                        {
                            if (dsdSetDic.ContainsKey(entity.Id))
                            {
                                dsdSetDic.Remove(entity.Id);
                            }
                            dsdSetRepository.Context.Rollback();
                            throw;
                        }
                    }
                }
                if (isCommand)
                {
                    acDomain.MessageDispatcher.DispatchMessage(new DsdSetAddedEvent(acSession, entity, input, isPrivate: true));
                }
            }
Ejemplo n.º 5
0
 private void Init()
 {
     if (_initialized)
     {
         return;
     }
     lock (Locker)
     {
         if (_initialized)
         {
             return;
         }
         _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitingEvent(this));
         _dsdSetDic.Clear();
         _dsdRoleBySet.Clear();
         _dsdRoleById.Clear();
         var stateReder = _acDomain.RetrieveRequiredService <IOriginalHostStateReader>();
         var dsdSets    = stateReder.GetAllDsdSets();
         foreach (var dsdSet in dsdSets)
         {
             if (!_dsdSetDic.ContainsKey(dsdSet.Id))
             {
                 _dsdSetDic.Add(dsdSet.Id, DsdSetState.Create(dsdSet));
             }
         }
         var dsdRoles = stateReder.GetAllDsdRoles();
         foreach (var dsdRole in dsdRoles)
         {
             DsdSetState dsdSetState;
             if (_dsdSetDic.TryGetValue(dsdRole.DsdSetId, out dsdSetState))
             {
                 var state = DsdRoleState.Create(dsdRole);
                 if (!_dsdRoleById.ContainsKey(dsdRole.Id))
                 {
                     _dsdRoleById.Add(dsdRole.Id, state);
                 }
                 if (!_dsdRoleBySet.ContainsKey(dsdSetState))
                 {
                     _dsdRoleBySet.Add(dsdSetState, new List <DsdRoleState>());
                 }
                 _dsdRoleBySet[dsdSetState].Add(state);
             }
             else
             {
                 // TODO:删除非法的记录
             }
         }
         _initialized = true;
         _acDomain.MessageDispatcher.DispatchMessage(new MemorySetInitializedEvent(this));
     }
 }
Ejemplo n.º 6
0
            private void Handle(IAcSession acSession, IDsdSetUpdateIo input, bool isCommand)
            {
                var         acDomain         = _set._acDomain;
                var         dsdSetRepository = acDomain.RetrieveRequiredService <IRepository <DsdSet> >();
                DsdSetState bkState;

                if (!acDomain.DsdSetSet.TryGetDsdSet(input.Id, out bkState))
                {
                    throw new NotExistException();
                }
                DsdSet entity;
                bool   stateChanged;

                lock (Locker)
                {
                    DsdSetState oldState;
                    if (!acDomain.DsdSetSet.TryGetDsdSet(input.Id, out oldState))
                    {
                        throw new NotExistException();
                    }
                    if (acDomain.DsdSetSet.Any(a => a.Name.Equals(input.Name, StringComparison.OrdinalIgnoreCase) && a.Id != input.Id))
                    {
                        throw new ValidationException("重复的静态责任分离角色组名");
                    }
                    entity = dsdSetRepository.GetByKey(input.Id);
                    if (entity == null)
                    {
                        throw new NotExistException();
                    }

                    entity.Update(input);

                    var newState = DsdSetState.Create(entity);
                    stateChanged = newState != bkState;
                    if (stateChanged)
                    {
                        Update(newState);
                    }
                    if (isCommand)
                    {
                        try
                        {
                            dsdSetRepository.Update(entity);
                            dsdSetRepository.Context.Commit();
                        }
                        catch
                        {
                            if (stateChanged)
                            {
                                Update(bkState);
                            }
                            dsdSetRepository.Context.Rollback();
                            throw;
                        }
                    }
                }
                if (isCommand && stateChanged)
                {
                    acDomain.MessageDispatcher.DispatchMessage(new DsdSetUpdatedEvent(acSession, entity, input, isPrivate: true));
                }
            }