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/了解更多详情。