C# 使用实体框架、代码优先和CRUD操作的存储库模式
我试图对如何正确实现存储库模式有一个实际的理解。我在MVC web应用程序中创建了一个名为Employees的模型类,我创建了一个上下文类和一个connectionstring来生成数据库。我还使用实体框架创建了一个具有读/写操作的控制器,其中包含一些CRUD操作,如更新、删除等 如果我正确理解了存储库模式,我应该将所有数据访问逻辑都放在存储库中。我还认为我需要为上下文类创建一个IRepository接口来继承它 在我的控制器中,我有这些基本的CRUD操作。在我的例子中,是否应该将这些操作方法中的所有逻辑移到我的存储库类中 控制器:C# 使用实体框架、代码优先和CRUD操作的存储库模式,c#,asp.net-mvc,entity-framework,repository-pattern,data-access-layer,C#,Asp.net Mvc,Entity Framework,Repository Pattern,Data Access Layer,我试图对如何正确实现存储库模式有一个实际的理解。我在MVC web应用程序中创建了一个名为Employees的模型类,我创建了一个上下文类和一个connectionstring来生成数据库。我还使用实体框架创建了一个具有读/写操作的控制器,其中包含一些CRUD操作,如更新、删除等 如果我正确理解了存储库模式,我应该将所有数据访问逻辑都放在存储库中。我还认为我需要为上下文类创建一个IRepository接口来继承它 在我的控制器中,我有这些基本的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);
}
}
public class CustomerRepository : _dbCrudApplicationContext
{
_dbCrudApplicationContext db = new _dbCrudApplicationContext();
public List<Employee> GetAllEmployees()
{
return db.Employees.ToList();
}
}
我开始将“索引”方法添加到存储库中,但我不确定如何实现其他方法,因为它们更复杂。我应该移动控制器中动作方法中的所有逻辑还是只移动部分代码
存储库:
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);
}
}
public class CustomerRepository : _dbCrudApplicationContext
{
_dbCrudApplicationContext db = new _dbCrudApplicationContext();
public List<Employee> GetAllEmployees()
{
return db.Employees.ToList();
}
}
public类CustomerRepository:\u dbcrudaplicationcontext
{
_dbCrudApplicationContext db=new _dbCrudApplicationContext();
公共列表GetAllEmployees()
{
return db.Employees.ToList();
}
}
存储库不应尝试扩展模型上下文,而应使用此上下文执行某些操作。存储库定义为
存储库在域和数据映射层之间进行中介,就像内存中的域对象集合一样。客户机对象以声明方式构造查询规范,并将其提交到存储库以满足需求。对象可以添加到存储库中,也可以从存储库中移除,就像它们可以从简单的对象集合中移除一样,存储库封装的映射代码将在幕后执行适当的操作
接口风格背后的主要原因是允许进行单元测试
在您的情况下,存储库可能看起来像
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();
}
}
公共接口ICCustomerRepository
{
公共列表GetAllEmployees();
}
公共类CustomerRepository:ICCustomerRepository
{
私有的_dbCrudApplicationContext数据库;
公共CustomerRepository(_dbCrudApplicationContext)
{
db=上下文;
}
公共列表GetAllEmployees()
{
return db.Employees.ToList();
}
}
完成后,存储库应包含要在模型上执行的所有操作。这些需要从MVC视图中迁移
有关更多详细信息,请参阅。请在此处查看我的答案:。实体框架禁止使用存储库模式。这根本没有必要。你要做的就是让你的应用程序更难使用。