我想了解如何正確實現存儲庫模式。我在我的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