You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自定义实现IntegrityList的ToIntegrityListAsync扩展方法?

问题描述

我正在创建一个自定义List实现类IntegrityList<TSource>,用于强制执行特定业务规则,代码如下:

[Serializable]
public class IntegrityList<TSource> : List<TSource> where TSource : IEvent
{
    // ... 业务逻辑 ...
}

同时需要实现类似LINQ的ToList()和EF的ToListAsync()的自定义扩展方法。我已经完成了同步的ToIntegrityList()方法,但不知道如何实现异步版本ToIntegrityListAsync()。现有扩展方法代码如下:

public static class IntegrityListExtensions
{
    public static IntegrityList<TSource> ToIntegrityList<TSource>(this IEnumerable<TSource> source) where TSource : IEvent
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        return new IntegrityList<TSource>(source);
    }

    public static Task<IntegrityList<TSource>> ToIntegrityListAsync<TSource>(this IQueryable<TSource> source) where TSource : IEvent
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        throw new NotImplementedException("Missing implementation");
    }

    public static Task<IntegrityList<TSource>> ToIntegrityListAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken) where TSource : IEvent
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        else if (cancellationToken == null) throw new ArgumentNullException(nameof(cancellationToken));
        throw new NotImplementedException("Missing implementation");
    }
}
实现方案

要实现ToIntegrityListAsync,核心是借助EF Core的ToListAsync异步获取数据集合,再将结果转换为IntegrityList<TSource>。以下是完整实现:

完整扩展方法代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; // 需引入EF Core命名空间

public static class IntegrityListExtensions
{
    public static IntegrityList<TSource> ToIntegrityList<TSource>(this IEnumerable<TSource> source) where TSource : IEvent
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        return new IntegrityList<TSource>(source);
    }

    public static async Task<IntegrityList<TSource>> ToIntegrityListAsync<TSource>(this IQueryable<TSource> source) where TSource : IEvent
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        var dataList = await source.ToListAsync().ConfigureAwait(false);
        return new IntegrityList<TSource>(dataList);
    }

    public static async Task<IntegrityList<TSource>> ToIntegrityListAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken) where TSource : IEvent
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        cancellationToken.ThrowIfCancellationRequested();

        var dataList = await source.ToListAsync(cancellationToken).ConfigureAwait(false);
        return new IntegrityList<TSource>(dataList);
    }
}

关键细节说明

  • 依赖EF Core:ToListAsync是EF Core针对IQueryable提供的异步扩展方法,需确保项目已安装Microsoft.EntityFrameworkCore NuGet包。
  • 异步优化:使用ConfigureAwait(false)避免不必要的上下文切换,提升异步操作性能。
  • 取消令牌处理:CancellationToken是值类型,无需判断是否为null,直接调用ThrowIfCancellationRequested()即可在请求取消时抛出异常。
  • 复用构造逻辑:异步获取List<TSource>后,直接传入IntegrityList的构造函数,复用同步版本的业务规则校验逻辑。

内容的提问来源于stack exchange,提问作者Andreasx23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 10:40:35