c# - 使用异步/等待方法时使用存储库模式 Asp.NET MVC 5 EF

标签 c# entity-framework asynchronous asp.net-mvc-5

你能解释一下如何在使用 async/await 方法时实现存储库模式吗,这里是没有异步的例子:

型号:

public class Note
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
}

接口(interface):

interface INoteRepository : IDisposable
{
    IEnumerable<Note> GetNotes();
    Note GetNoteById(Guid? id);
    void PostNote(Note note);
    void DeleteNote(Guid id);
    void PutNote(Note note);
    void Save();
}

存储库:

public class NoteRepository : INoteRepository, IDisposable
{
    private MyDbContext context;

    public NoteRepository(MyDbContext _context)
    {
        context = _context;
    }
    public void DeleteNote(Guid id)
    {
        Note note = context.Notes.Find(id);
        context.Notes.Remove(note);
    }

    public Note GetNoteById(Guid id)
    {
        return context.Notes.Find(id);
    }

    public IEnumerable<Note> GetNotes()
    {
        return context.Notes.ToList();
    }

    public void PostNote(Note note)
    {
        context.Notes.Add(note);
    }

    public void PutNote(Note note)
    {
        context.Entry(note).State = EntityState.Modified;
    }

    public void Save()
    {
        context.SaveChanges();
    }

    #region IDisposable Support
    private bool disposedValue = false; // To detect redundant calls

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                // TODO: dispose managed state (managed objects).
                context.Dispose();
            }

            // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
            // TODO: set large fields to null.

            disposedValue = true;
        }
    }

    // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
    // ~NoteRepository() {
    //   // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
    //   Dispose(false);
    // }

    // This code added to correctly implement the disposable pattern.
    public void Dispose()
    {
        // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
        Dispose(true);
        // TODO: uncomment the following line if the finalizer is overridden above.
        // GC.SuppressFinalize(this);
        GC.SuppressFinalize(this);
    }

    public Note GetNoteById(Guid? id)
    {
        return context.Notes.Find(id);
    }
    #endregion

}

Controller :

public class NotesController : Controller
{
    //private MyDbContext db = new MyDbContext();

    private INoteRepository noterepository;

    //public NotesController(INoteRepository _noterepository)
    //{
    //    _noterepository = noterepository;
    //}

    public NotesController()
    {
        noterepository = new NoteRepository(new MyDbContext());
    }



    // GET: Notes
    public ActionResult Index()
    {
        return View(noterepository.GetNotes());
    }

    // GET: Notes/Details/5
    public ActionResult Details(Guid? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Note note = noterepository.GetNoteById(id);
        if (note == null)
        {
            return HttpNotFound();
        }
        return View(note);
    }

    // GET: Notes/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: Notes/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,Title,Body")] Note note)
    {
        if (ModelState.IsValid)
        {
            note.Id = Guid.NewGuid();
            noterepository.PostNote(note);
            noterepository.Save();
            return RedirectToAction("Index");
        }

        return View(note);
    }

    // GET: Notes/Edit/5
    public ActionResult Edit(Guid? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Note note = noterepository.GetNoteById(id);
        if (note == null)
        {
            return HttpNotFound();
        }
        return View(note);
    }

    // POST: Notes/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,Title,Body")] Note note)
    {
        if (ModelState.IsValid)
        {
            noterepository.PutNote(note);
            noterepository.Save();
            return RedirectToAction("Index");
        }
        return View(note);
    }

    // GET: Notes/Delete/5
    public ActionResult Delete(Guid? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Note note = noterepository.GetNoteById(id);
        if (note == null)
        {
            return HttpNotFound();
        }
        return View(note);
    }

    // POST: Notes/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(Guid id)
    {
        Note note = noterepository.GetNoteById(id);
        noterepository.DeleteNote(id);
        noterepository.Save();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            noterepository.Dispose();
        }
        base.Dispose(disposing);
    }
}

现在有一个问题: 我如何使用 acync/await 方法执行此操作,通过搜索我找到了一些示例,但我无法理解网络中没有完整的示例。 非常感谢。

最佳答案

您的新异步存储库类:

public class NoteRepository : INoteRepository, IDisposable
{
    private MyDbContext context;

    public NoteRepository(MyDbContext _context)
    {
        context = _context;
    }

    public async Task DeleteNoteAsync(Guid id)
    {
        Note note = await context.Notes.FindAsync(id);
        context.Notes.Remove(note);
    }

    public async Task<Note> GetNoteByIdAsync(Guid id)
    {
        return await context.Notes.FindAsync(id);
    }

    public async Task<List<Note>> GetNotesAsync()
    {
        return await context.Notes.ToListAsync();
    }

    ...

    public async Task SaveAsync()
    {
        await context.SaveChangesAsync();
    }

    ...

    public async Task<Note> GetNoteById(Guid? id)
    {
        return await context.Notes.FindAsync(id);
    }

    #endregion
}

变化:

  • 将每个返回值 T 转换为 async Task<T>
  • 在内部使用数据库异步操作
  • 在方法名称中添加“Async”后缀

你的新 Controller 类:

public class NotesController : Controller
{
    //private MyDbContext db = new MyDbContext();

    private INoteRepository noterepository;

    //public NotesController(INoteRepository _noterepository)
    //{
    //    _noterepository = noterepository;
    //}

    public NotesController()
    {
        noterepository = new NoteRepository(new MyDbContext());
    }



    // GET: Notes
    public async Task<ActionResult> Index()
    {
        return await View(noterepository.GetNotesAsync());
    }

    // GET: Notes/Details/5
    public async Task<ActionResult> Details(Guid? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Note note = await noterepository.GetNoteByIdAsync(id);
        if (note == null)
        {
            return HttpNotFound();
        }
        return View(note);
    }

    // GET: Notes/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: Notes/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "Id,Title,Body")] Note note)
    {
        if (ModelState.IsValid)
        {
            note.Id = Guid.NewGuid();
            await noterepository.PostNoteAsync(note);
            await noterepository.SaveAsync();
            return RedirectToAction("Index");
        }

        return View(note);
    }

    // GET: Notes/Edit/5
    public async Task<ActionResult> Edit(Guid? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Note note = await noterepository.GetNoteByIdAsync(id);
        if (note == null)
        {
            return HttpNotFound();
        }
        return View(note);
    }

    // POST: Notes/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Edit([Bind(Include = "Id,Title,Body")] Note note)
    {
        if (ModelState.IsValid)
        {
            await noterepository.PutNoteAsync(note);
            await noterepository.SaveAsync();
            return RedirectToAction("Index");
        }
        return View(note);
    }

    // GET: Notes/Delete/5
    public async Task<ActionResult> Delete(Guid? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Note note = await noterepository.GetNoteByIdAsync(id);
        if (note == null)
        {
            return HttpNotFound();
        }
        return View(note);
    }

    // POST: Notes/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> DeleteConfirmed(Guid id)
    {
        Note note = await noterepository.GetNoteByIdAsync(id);
        await noterepository.DeleteNoteAsync(id);
        await noterepository.SaveAsync();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            noterepository.Dispose();
        }
        base.Dispose(disposing);
    }
}

变化:

  • 将每个返回值 T 转换为 async Task<T>
  • 在内部使用 noterepository 异步操作

关于c# - 使用异步/等待方法时使用存储库模式 Asp.NET MVC 5 EF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37724049/

相关文章:

c# - Mongo Atlas - 连接限制低?

c# - 如何找到 MemoryCache 的大小,最好是通过编程方式?

c# - 哪种空间数据结构(算法)最适合(搜索)一组区域(空间数据)?

c# - Entity Framework 为包含属性返回 null

android - Kotlin 中的异步匿名函数? ( lambda 表达式)

c# - Microsoft VSrollBar 行为或触摸屏错误 - Windows 窗体

c# - 在对象图中查找具有相同键的实体以防止 "An object with the same key already exists in the ObjectStateManager"错误

mysql - 如何使用 Entity Framework 6 从 ASP.NET 5 Web API 应用程序连接到 MySql?

angular - 在服务 http.get 调用完成后让组件执行函数

javascript - Console.log 异步 Facebook Like 按钮加载(显示)后的内容