/// <exception cref="DatabaseException"></exception>
        private async Task ConvertLongIdAsync(object obj, long id, PropertyInfo propertyInfo, ApiRequestType requestType, ChangeDirection direction, string requestId)
        {
            if (id < 0)
            {
                return;
            }

            if (propertyInfo.Name == nameof(ApiResource.Id) && requestType == ApiRequestType.Add && direction == ChangeDirection.ToServer)
            {
                _addRequestClientIdDict[requestId].Add(id);

                propertyInfo.SetValue(obj, -1);

                return;
            }

            long changedId = direction switch
            {
                ChangeDirection.ToServer => await _idBarrierRepo.GetServerIdAsync(id).ConfigureAwait(false),
                ChangeDirection.FromServer => await _idBarrierRepo.GetClientIdAsync(id).ConfigureAwait(false),
                _ => - 1,
            };

            if (direction == ChangeDirection.FromServer &&
                changedId < 0 &&
                id > 0 &&
                (requestType == ApiRequestType.Get || requestType == ApiRequestType.GetSingle))
            {
                changedId = StaticIdGen.GetId();
                await AddServerIdToClientIdAsync(id, changedId).ConfigureAwait(false);
            }
            //如果服务器返回Id=-1,即这个数据不是使用Id来作为主键的,客户端实体应该避免使用IdGenEntity

            propertyInfo.SetValue(obj, changedId);
        }
예제 #2
0
        /// <summary>
        /// GenerateIdAsync
        /// </summary>
        /// <param name="dict"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        private static Task GenerateIdAsync(ConcurrentDictionary<long, int> dict)
        {
            Task task = new Task(() =>
            {
                int taskID = Task.CurrentId!.Value;
                for (int i = 0; i < 10; ++i)
                {
                    long id = StaticIdGen.GetId();
                    Debug.WriteLine($"{id}    {taskID}");
                    if (!dict.TryAdd(id, taskID))
                    {
#pragma warning disable CA2201 // Do not raise reserved exception types
                        throw new Exception("duplicated");
#pragma warning restore CA2201 // Do not raise reserved exception types
                    }
                }
            });

            task.Start();

            return task;

        }