使用async/await方法Asp.NET MVC5 EF时使用repository patern

Arc*_*dze 7 c# asynchronous entity-framework 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; }
}
Run Code Online (Sandbox Code Playgroud)

接口:

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

库:

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

}
Run Code Online (Sandbox Code Playgroud)

控制器:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在这里有一个问题:如何使用acync/await方法执行此操作,从搜索中我发现了一些示例,但我不明白网络中没有完全的问题.非常感谢.

omi*_*kad 17

您的新异步存储库类:

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
}
Run Code Online (Sandbox Code Playgroud)

变化:

  • 将每个返回值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);
    }
}
Run Code Online (Sandbox Code Playgroud)

变化:

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

  • 我希望数据访问层返回IEnumerable <T>,因为我能够在EF生成的查询中堆叠.where(...)和其他子句; 当您创建.ToListAsync()时,您将获取整个数据集,如果使用.where(...)子句应用数据访问中的方法,则过滤将在.NET中进行.如果在MSSQL内部进行过滤,则性能会更好. (2认同)