3

我想了解如何正確實現存儲庫模式。我在我的MVC Web應用程序中創建了一個名爲Employees的模型類,iv'e創建了一個上下文類和一個連接字符串以生成數據庫。我還創建了一個使用實體框架工作的讀/寫操作的控制器,它帶來了一些CRUD操作,例如更新,刪除等。使用實體框架,代碼優先和CRUD操作的存儲庫模式

如果我已經正確理解存儲庫模式,我應該將所有數據訪問邏輯放入存儲庫。相信我需要爲上下文類創建一個Irepositorey接口來繼承它。

在我的控制器中,我有這些基本的CRUD操作。在我的情況下,這些操作方法中的所有邏輯是否應該移動到我的存儲庫類中?

控制器:

public class EmployeeController : Controller 
    { 
     private _dbCrudApplicationContext db = new _dbCrudApplicationContext(); 

     // GET: Employee 
     public ActionResult Index() 
     { 
      return View(db.Employees.ToList()); 
     } 

     // GET: Employee/Details/5 
     public ActionResult Details(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Employee employee = db.Employees.Find(id); 
      if (employee == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(employee); 
     } 

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

     // POST: Employee/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,FirstName,LastName,EmployeeNumber,Department,HasValidEmployeeCertificate")] Employee employee) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Employees.Add(employee); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(employee); 
     } 

     // GET: Employee/Edit/5 
     public ActionResult Edit(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Employee employee = db.Employees.Find(id); 
      if (employee == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(employee); 
     } 

     // POST: Employee/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,FirstName,LastName,EmployeeNumber,Department,HasValidEmployeeCertificate")] Employee employee) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(employee).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(employee); 
     } 

     // GET: Employee/Delete/5 
     public ActionResult Delete(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Employee employee = db.Employees.Find(id); 
      if (employee == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(employee); 
     } 

     // POST: Employee/Delete/5 
     [HttpPost, ActionName("Delete")] 
     [ValidateAntiForgeryToken] 
     public ActionResult DeleteConfirmed(int id) 
     { 
      Employee employee = db.Employees.Find(id); 
      db.Employees.Remove(employee); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

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

伊夫開始加入了「指數」的方法到倉庫,但我不確定如何實現其他方法,因爲他們更復合。我應該移動Controller中的操作方法中的所有邏輯還是僅移動代碼的一部分?

庫:

public class CustomerRepository : _dbCrudApplicationContext 
    { 
     _dbCrudApplicationContext db = new _dbCrudApplicationContext(); 


     public List<Employee> GetAllEmployees() 
     { 
      return db.Employees.ToList();  
     } 
    } 

對不起,我在新的這裏。將感謝一些輸入。

//克里斯

+1

請在這裏看到我的答案:http://programmers.stackexchange.com/questions/180851/why-shouldnt-i-use-the-repository-pattern-with-entity-framework/220126#220126。實體框架排除了使用存儲庫模式。這根本就沒有必要。你所要做的只是讓你的應用程序更難處理。 – 2014-09-01 17:45:53

回答

0

信息庫不應該試圖擴展模式下,而應當使用此背景下執行某些操作。阿庫被定義爲

阿庫域和數據映射層間介導,作用像一個內存域對象集合。客戶端對象以聲明方式構造查詢規範並將其提交給Repository以滿足需求。對象可以被添加到和移除從存儲庫,因爲它們可以從對象的簡單集合,並且由存儲庫封裝的映射代碼將執行適當的操作幕後

主要接口風格背後的原因是允許進行單元測試。

在你的情況下,資源庫可能看起來像

public interface ICustomerRepository 
{ 
    public List<Employee> GetAllEmployees(); 
} 

public class CustomerRepository : ICustomerRepository 
{ 
    private _dbCrudApplicationContext db; 

    public CustomerRepository(_dbCrudApplicationContext context) 
    { 
    db = context; 
    } 

    public List<Employee> GetAllEmployees() 
    { 
     return db.Employees.ToList(); 
    } 
} 

存儲庫時完成應包含要在模型上進行的所有操作。這些將需要從MVC視圖中遷移。

請參閱http://blog.gauffin.org/2013/01/repository-pattern-done-right/瞭解更多詳情。