Tuesday, July 28, 2015

Entity FrameWork Repository FrameWork 2

We earlier saw how to implement single Repository for all the entity type.
now i would like to show how create a repository and use one for each of the entity type

As usual Create a Model Class




    public class PurchaseOrder
    {
        [Key]
        public int PoNumber { get; set; }
        public decimal Quantity { get; set; }
        public decimal  Rate { get; set; }
        public decimal PoValue { get; set; }

    }


In The Repository folder Create an interface 

    interface IpoRepository:IDisposable
    {
        IEnumerable<PurchaseOrder> GetOrders();
        PurchaseOrder GetOrdersById(int PoNumber);
        void InsertPO(PurchaseOrder PO);
        void DeletePO(int PoNumber);
        void UpdatePo(PurchaseOrder PO);

    }

This above code defines methods for your CRUD Operations

Create a Class which implements this interface

    public class PORepository:IpoRepository,IDisposable
    {
        Models.POContext PoContext = new Models.POContext();

        public IEnumerable<Models.PurchaseOrder> GetOrders()
        {

            return PoContext.PurchaseOrders.ToList();

        }

        public Models.PurchaseOrder GetOrdersById(int PoNumber)
        {

            return PoContext.PurchaseOrders.Find(PoNumber);

        }

        public void InsertPO(Models.PurchaseOrder PO)
        {
            PoContext.PurchaseOrders.Add(PO);

        }

        public void DeletePO(int PoNumber)
        {

            PurchaseOrder Po = PoContext.PurchaseOrders.Find(PoNumber);
           
            PoContext.PurchaseOrders.Remove(Po);

            
        }

        public void UpdatePo(Models.PurchaseOrder PO)
        {
            PoContext.Entry(PO).State = EntityState.Modified;
          
        }

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

        public void Dispose()
        {
            Dispose();
            GC.SuppressFinalize(this);

        }
    }

Create a Context class

public class POContext:DbContext
    {
      public  DbSet<PurchaseOrder> PurchaseOrders { get; set; }

    }

Scaffold out the Controller and replace the code inside it to use Repository 

    public class POController : Controller
    {
        private POContext db = new POContext();
        private PORepository repo = new PORepository();


        // GET: /PO/
        public  ActionResult Index()
        {
            return View(repo.GetOrders().ToList());
        }

        // GET: /PO/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            PurchaseOrder purchaseorder = await db.PurchaseOrders.FindAsync(id);
            if (purchaseorder == null)
            {
                return HttpNotFound();
            }
            return View(purchaseorder);
        }

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

        // POST: /PO/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="PoNumber,Quantity,Rate,PoValue")] PurchaseOrder purchaseorder)
        {
            if (ModelState.IsValid)
            {
                repo.InsertPO(purchaseorder);
               // db.PurchaseOrders.Add(purchaseorder);
                repo.Save();
                return RedirectToAction("Index");
            }

            return View(purchaseorder);
        }

        // GET: /PO/Edit/5
        public ActionResult Edit(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            PurchaseOrder purchaseorder = repo.GetOrdersById(id);
            if (purchaseorder == null)
            {
                return HttpNotFound();
            }
            return View(purchaseorder);
        }

        // POST: /PO/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="PoNumber,Quantity,Rate,PoValue")] PurchaseOrder purchaseorder)
        {
            if (ModelState.IsValid)
            {
                repo.UpdatePo(purchaseorder);
                repo.Save();
               
                return RedirectToAction("Index");
            }
            return View(purchaseorder);
        }

        // GET: /PO/Delete/5
        public ActionResult Delete(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            PurchaseOrder purchaseorder = repo.GetOrdersById(id);
            if (purchaseorder == null)
            {
                return HttpNotFound();
            }
            return View(purchaseorder);
        }

        // POST: /PO/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            PurchaseOrder purchaseorder = repo.GetOrdersById(id);
            repo.DeletePO(id);
            repo.Save();
          //  db.PurchaseOrders.Remove(purchaseorder);
         
            return RedirectToAction("Index");
        }

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

Run the Solution and see the magic 
it works like charm. exactly the way it used to work even without repository .but now the code is more cleaner and abstraction level between DataAccess Layer and Business Logic is good. 



No comments:

Post a Comment