MVC + 3层; DAL和viewmodels

时间:2015-10-26 12:35:26

标签: asp.net-mvc data-access-layer

我正在将一个普通的单层网店变成一个4层。

问题在于DAL方法返回viewmodels或将viewmodels作为参数。以下是我的想法,但由于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

I'm following this diagram.

1 个答案:

答案 0 :(得分:1)

您的域层(客户)应在模型库中定义(左侧)。然后你的BLL和DLL应该引用Models库并使用这个对象来实现业务逻辑和持久性。

您的视图模型应转换为表示层的Model实例。