Ejemplo n.º 1
0
        /// <summary>
        /// 异步设置标识。
        /// </summary>
        /// <typeparam name="TId">指定的标识类型(兼容各种引用与值类型标识)。</typeparam>
        /// <param name="identifier">给定的 <see cref="IIdentifier{TId}"/>。</param>
        /// <param name="newIdFactory">给定的新 <typeparamref name="TId"/> 工厂方法。</param>
        /// <param name="cancellationToken">给定的 <see cref="CancellationToken"/>(可选)。</param>
        /// <returns>返回一个包含 <typeparamref name="TId"/> (兼容各种引用与值类型标识)的异步操作。</returns>
        public static ValueTask <TId> SetIdAsync <TId>(this IIdentifier <TId> identifier,
                                                       Func <TId, TId> newIdFactory, CancellationToken cancellationToken = default)
            where TId : IEquatable <TId>
        {
            identifier.NotNull(nameof(identifier));
            newIdFactory.NotNull(nameof(newIdFactory));

            return(cancellationToken.RunOrCancelValueAsync(()
                                                           => identifier.Id = newIdFactory.Invoke(identifier.Id)));
        }