MVC_part1

  • 116
  • 0
  • MVC
  • 2018-07-31

MVC_part1

MVC_part1

單一職責原則(Single Responsibility Principle,SRP)

Buld MVC Project

1.create Project

2.Add ADO.NET Entity Framework

3.create controller

4.add Repository    -> 把資料庫操作方法從 Action 方法給抽離出來,通常都會使用一種設計模式「Repository Pattern(倉儲模式)」

5.instance interface   ->最後實作了 IDispose 介面,讓資料操作完成後可以釋放資源,

using MVCProject.Models.Interface;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MVCProject.Models.Repository
{
    public class CompanyReposity : ICompanyRepository, IDisposable
    {

        protected HoEntities db { get; private set; }

        public CompanyReposity()
        {
            this.db = new HoEntities();
        }

        public void Create(Company company)
        {
            if (company == null)
            {
                throw new ArgumentNullException("instance");
            }
            else
            {
                db.Companies.Add(company);
                db.SaveChanges();
            }
        }

        public void Update(Company company)
        {
            if (company == null)
            {
                throw new ArgumentNullException();
            }
            else
            {
                db.Entry(company).State = EntityState.Modified;
                this.SaveChanges();
            }
        }

        public void Delete(Company company)
        {
            if (company == null)
            {
                throw new ArgumentNullException();
            }
            else
            {
                db.Entry(company).State = EntityState.Deleted;
                this.SaveChanges();
            }
        }

        public Company Get(int companyID)
        {
            return db.Companies.FirstOrDefault(x => x.CompanyID == companyID);
        }

        public IQueryable<Company> GetAll()
        {
            return db.Companies.OrderBy(x => x.CompanyID);
        }

        public void SaveChanges()
        {
            this.db.SaveChanges();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.db != null)
                {
                    this.db.Dispose();
                    this.db = null;
                }
            }
        }

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

6.use in Controller  -> 先建立一個類別為 ICategoryRepository 的私有欄位,然後在建構式中再以 CategoryRepository 類別去建立 instance,

 // GET: Companies
        public ActionResult Index()
        {
            var companies = this.CompanyRespository.GetAll().ToList();
            return View(companies);
        }
// GET: Companies/Details/5
        public ActionResult Details(int? id)
        {
            if (!id.HasValue)
            {
                return RedirectToAction("index");
            }
            else
            {
                var company = this.CompanyRespository.Get(id.Value);
                return View(company);
            }
        }
// GET: Companies/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Companies/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Company company)
        {
            if (company != null && ModelState.IsValid)
            {
                this.CompanyRespository.Create(company);
                return RedirectToAction("index");
            }
            else
            {
                return View(company);
            }
        }
// GET: Companies/Edit/5
        public ActionResult Edit(int? id)
        {
            if (!id.HasValue)
            {
                return RedirectToAction("index");
            }
            else
            {
                var company = this.CompanyRespository.Get(id.Value);
                return View(company);
            }
        }

        // POST: Companies/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit( Company company)
        {
            if (company!=null && ModelState.IsValid)
            {
                this.CompanyRespository.Update(company);
                return View(company);
            }
            else
            {
                return RedirectToAction("index");
            }
        }
// GET: Companies/Delete/5
        public ActionResult Delete(int? id)
        {
            if (!id.HasValue)
            {
                return RedirectToAction("index");
            }
            else
            {
                var company = this.CompanyRespository.Get(id.Value);
                return View(company);
            }
        }

        // POST: Companies/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                var company = this.CompanyRespository.Get(id);
                this.CompanyRespository.Delete(company);
            }
            catch (DataException)
            {
                return RedirectToAction("Delete", new { id = id });
            }
            return RedirectToAction("index");
        }

 

參考網址: IT 、mvc-part1