Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using repository patern when using async / await methods Asp.NET MVC5 EF

Can you explane me how to implement repository patterns when using async / await methods, here is example without async:

Model:

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();
}

Repository:

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);
    }
}

Now here is a question: How can I do this with acync / await methods, from searching I found some examples but I cannon understand there is no full exaple in network. Lot of thanks.

like image 305
Archil Labadze Avatar asked Jun 09 '16 10:06

Archil Labadze


1 Answers

Your new async Repository class:

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
}

Changes:

  • Convert every return value T to async Task<T>
  • Use data base async operations inside
  • Add 'Async' suffix to method names

Your new Controller class:

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);
    }
}

Changes:

  • Convert every return value T to async Task<T>
  • Use noterepository async operations inside
like image 192
omikad Avatar answered Oct 02 '22 04:10

omikad