我想了解如何正确实现存储库模式。我在我的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();
}
}
对不起,我在新的这里。将感谢一些输入。
//克里斯
请在这里看到我的答案:http://programmers.stackexchange.com/questions/180851/why-shouldnt-i-use-the-repository-pattern-with-entity-framework/220126#220126。实体框架排除了使用存储库模式。这根本就没有必要。你所要做的只是让你的应用程序更难处理。 – 2014-09-01 17:45:53