MVC+3层; DAL 和视图模型
MVC + 3 layer; DAL and viewmodels
我正在将普通的 1 层网上商店改成 4 层。
问题在于 return 视图模型或将视图模型作为参数的 DAL 方法。以下是我的想法,但它不会工作,因为 DAL 无法访问视图模型。
MVC: reference BLL and Model
Controllers/
CustomerController.cs
[HttpPost]
public ActionResult Create(CreateAccountViewModel c) {
var logic = new CustomerLogic();
logic.CreateAccount(c);
return RedirectToAction("Success");
}
Views/Customer/
CreateAccount.cshtml
ViewModels/
CreateAccountViewModel.cs
BLL: reference DAL, Model and MVC
CustomerLogic.cs
public void CreateAccount(CreateAccountViewModel c) {
var db = new DBCustomer();
db.createAccount(c);
}
DAL: reference Model and BLL
DBContext.cs
DBCustomer.cs
public void CreateAccount(CreateAccountViewModel c) {
var db = DBContext;
var newCust = new Customer() {
FirstName = c.FirstName,
LastName = c.LastName
}
db.Add(newCust);
}
Model:
Customer.cs
您的域层(客户)应该在模型库(左边的旁边)中定义。那么您的 BLL 和 DLL 应该引用模型库并将此对象用于业务逻辑和持久性。
您的视图模型应转换为表示层的模型实例。
我正在将普通的 1 层网上商店改成 4 层。
问题在于 return 视图模型或将视图模型作为参数的 DAL 方法。以下是我的想法,但它不会工作,因为 DAL 无法访问视图模型。
MVC: reference BLL and Model
Controllers/
CustomerController.cs
[HttpPost]
public ActionResult Create(CreateAccountViewModel c) {
var logic = new CustomerLogic();
logic.CreateAccount(c);
return RedirectToAction("Success");
}
Views/Customer/
CreateAccount.cshtml
ViewModels/
CreateAccountViewModel.cs
BLL: reference DAL, Model and MVC
CustomerLogic.cs
public void CreateAccount(CreateAccountViewModel c) {
var db = new DBCustomer();
db.createAccount(c);
}
DAL: reference Model and BLL
DBContext.cs
DBCustomer.cs
public void CreateAccount(CreateAccountViewModel c) {
var db = DBContext;
var newCust = new Customer() {
FirstName = c.FirstName,
LastName = c.LastName
}
db.Add(newCust);
}
Model:
Customer.cs
您的域层(客户)应该在模型库(左边的旁边)中定义。那么您的 BLL 和 DLL 应该引用模型库并将此对象用于业务逻辑和持久性。
您的视图模型应转换为表示层的模型实例。