Thursday 4 February 2021

Part 21: Dependency injection with Repository pattern

#10 minutes of reading
I’m continue this article from part 20 article of mini project, please visit Part 20 before this. In previous article I have shown accessing DbContext object directly in controller class, but in real time project we usually avoid it. Lets clean up project by deleting controller and respective views. Sample code available on github.
Delete everything from MobileBrands Controller highlighted below.
Deleted MobileBrands folder from Views folder.
Move MobileDbContext.cs file from Models folder to root directory of project.
Add two more folder under project directory.
  Add interface IMobileBrands.
public interface IMobileBrands { List FindAll(); MobileBrand FindById(int id); bool Create(MobileBrand entity); bool Update(MobileBrand entity); bool Delete(MobileBrand entity); bool Save(); }

Add below repository and inject DbContext to access database.

Add Mapping class under root directory of project, this file is used for dependency injection mapping of contacts and repository of entity, leave it for now you will understand soon why this class is created.
Update Startup.cs file and call above DependencyMapping method in ConfigureServices of Startup class.
Add controller with below highlighted option.
  Controller gets created using default empty actions.
Update Mobile brand controller with below code. public class MobileBrandController : Controller { readonly IMobileBrands _brand; public MobileBrandController(IMobileBrands brand) { _brand = brand; } // GET: MobileBrandController public ActionResult Index() { var data = _brand.FindAll(); return View(data); } // GET: MobileBrandController/Details/5 public ActionResult Details(int id) { var data = _brand.FindById(id); return View(data); } // GET: MobileBrandController/Create public ActionResult Create() { return View(); } // POST: MobileBrandController/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(MobileBrand entity) { try { _brand.Create(entity); return RedirectToAction(nameof(Index)); } catch(Exception ex) { return View(); } } // GET: MobileBrandController/Edit/5 public ActionResult Edit(int id) { return View(_brand.FindById(id)); } // POST: MobileBrandController/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(int id, MobileBrand entity) { try { _brand.Update(entity); return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: MobileBrandController/Delete/5 public ActionResult Delete(int id) { return View(); } // POST: MobileBrandController/Delete/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Delete(int id, MobileBrand entity) { try { _brand.Delete(entity); return RedirectToAction(nameof(Index)); } catch { return View(); } } }   Add Index view for MobileBrand controller with below options shown.
Also create the view for remaining action (Details, Edit, …etc.) Open Index View of Mobile brand and update below highlighted action link with proper Id.
Remove highlighted code from Edit shown below.
Run application and check in browser window to perform all CRUD operation for Brand from browser.
Previous

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home