如何将 razor pages 项目中的 Ienumerable 存储库调用异步转换为 运行

How can I convert an Ienumerable repository call in a razor pages project to run asynchronously

我有一个名为 notes 的模型,我通过调用接口/存储库 class 将其输入 kendo 网格。一切正常,但它是 运行 同步的,我想 运行 它是异步的。 我正在使用 .NET 核心 3.1,所以如果我能弄清楚如何去做的话,IAsyncEnumerable 等应该都可用。我尝试了很多变体,但总是出错。非常感谢任何帮助。

这是界面

namespace FliveRetry.Models.PTs
{
    public  interface IPtNoteRepository
    {       
        IEnumerable<Note> GetAllNotes();

        Note GetNoteById(int NoteId);

    }
}

这是存储库


namespace FliveRetry.Models.PTs
{
    public class PtNoteRepository : IPtNoteRepository
    {
        private readonly FliveRetryContext context;

        public PtNoteRepository(FliveRetryContext context)
        {
            this.context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public IEnumerable<Note> GetAllNotes()
        {
            return context.Note;
        }

        public Note GetNoteById(int itemId)
        {
            var note = context.Note.SingleOrDefault(i => i.ID == itemId);
            return note;
        }

    }
}

这是我调用它并通过 OnPostRead

将其提供给网格的索引模型

namespace FliveRetry.Pages.Notes
{
    public class IndexModel : NoteSelectPageModel
    {
        private const int CURRENT_USER_ID = 21; //Fake user id for demo

        private readonly IPtNoteRepository rpsNotesRepo;
        public static IList<Note> Notes { get; set; }

        [BindProperty(SupportsGet = true)]
        public NoteScreenEnum? PresetScreen { get; set; }

        public IndexModel(IPtNoteRepository rpsNotesData)
        {
            rpsNotesRepo = rpsNotesData;
        }

        public void OnGet()
        {           
            IEnumerable<Note> notes;
            switch (PresetScreen)
            {
                case NoteScreenEnum.GeneralNotes:
                    notes = rpsNotesRepo.GetAllNotes();
                    break;
                case NoteScreenEnum.ThisNote:
                    notes = rpsNotesRepo.GetNoteByID(CURRENT_USER_ID);
                    break;              
                default:
                    notes = rpsNotesRepo.GetAllNotes();
                    break;
            }
            Notes = notes.ToList();
        }

        public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request)
        {           
            return new JsonResult(Notes.ToDataSourceResult(request));
        }
    }
}

在创建或 edit.cshtml.cs 等其他页面中,我成功地使用异步进行编辑和创建,例如:


namespace FliveRetry.Pages.Notes
{
    public class EditModel : NoteSelectPageModel
    {
        private readonly FliveRetry.Data.FliveRetryContext _context;


        public EditModel(FliveRetry.Data.FliveRetryContext context)
        {
            _context = context;
        }

        [BindProperty]
        public Note Note { get; set; }

        public async Task<IActionResult> OnGetAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            Note = await _context.Note
                .AsNoTracking()
                .FirstOrDefaultAsync(m => m.ID == id);

            if (Note == null)
            {
                return NotFound();
            }
            return Page();
        }

        public async Task<IActionResult> OnPostAsync(IFormCollection form, int? id, string[] selectedOrgs, string[] selectedClients, string[] selectedStaffs, string[] selectedNoteTypes)
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            var noteToUpdate = await _context.Note              
                .FirstOrDefaultAsync(s => s.ID == id);
            if (await TryUpdateModelAsync<Note>(noteToUpdate, "note",   // Prefix for form value.
                   c => c.Title, c => c.NoteText, c => c.NoteDate, c => c.Amount, c => c.ImageURL, c => c.FileURL, c => c.Archived, c => c.DateSaved, c => c.UserID, c => c.StartTime, c => c.FinishTime))
            {               
                await _context.SaveChangesAsync();              
                return RedirectToPage("./Index");
            }
            return Page();
        }

    }
}

尝试使用以下代码将同步操作转换为异步操作:

IPtNoteRepository:

public  interface IPtNoteRepository
{       
    Task<IEnumerable<Note>> GetAllNotesAsync();
    Task<Note> GetNoteByIdAsync(int NoteId);
}

存储库:

public class PtNoteRepository : IPtNoteRepository
{
    private readonly FliveRetryContext context;

    public PtNoteRepository(FliveRetryContext context)
    {
        this.context = context ?? throw new ArgumentNullException(nameof(context));
    }

    public async Task<IEnumerable<Note>> GetAllNotesAsync()       
    {
        return await context.Note.ToListAsync();
    }

    public async Task<Note> GetNoteByIdAsync(int itemId)
    {
        var note = await context.Note.SingleOrDefaultAsync(i => i.ID == itemId);
        return note;
    }

}

索引模型:

public async Task OnGetAsync()
    {           
        IEnumerable<Note> notes;
        switch (PresetScreen)
        {
            case NoteScreenEnum.GeneralNotes:
                notes = await rpsNotesRepo.GetAllNotesAsync();
                break;
            case NoteScreenEnum.ThisNote:
                notes = await rpsNotesRepo.GetNoteByIdAsync(CURRENT_USER_ID);
                break;              
            default:
                notes = await rpsNotesRepo.GetAllNotesAsync();
                break;
        }
        Notes = notes.ToList();
    }