View Model Location in N Tier ASP.NET MVC Application

I read this one and wondered about it.

My app contains 4 layers

  • Web project / user interface
  • BLL
  • DAL (contains EF)
  • Essence level

I have placed the VM in the UI layer for now and a combination of different classes. something like that

    public class CompanyVMIndex
    {
       public CompanyVM Company { get; set; }
       public BillingAddressVM BillingAddress { get; set; }
       public List<ShippingAddressVM> ShippingAddress { get; set; }
       public List<CompanyContactVM> CompanyContact { get; set; }
    }

      

Now I am confused that I can send this data from the UI to the BLL and then to the DAL. I have read automapper but it handles situations like this, if so how? At this point I have decided to move the VMs to the Entity Layer that will be connected to all three layers so that I can send and receive data in the same one, any other good idea?

This is how I pass data from UI to BLL

             public ActionResult Create(CompanyVMIndex companyVM)
             {
               if (ModelState.IsValid)
                  {
                     //Calling BLL here
                     BLLFunction(companyVM)

                   }

                    return View("Index");
            }

      

then in BLL and something similar in DAL with Automapper

    public int BLLfunction(CompanyVMIndex CompanyVM)
    {

    }

      

now how can i transfer data as BLL has no definition of CompanyVMIndex which is virtual machine and web user interface

+3


source to share


1 answer


If you want to be "clean" then the ViewModel (or more generally any model you submit to view) will never see your BLL or any other layer. It will only be used for communication between controllers and views.

When the time comes to fetch data from the BLL or send data back to the BLL, other classes will be used. The data will be copied to and from the ViewModel classes.



So the ViewModel contains exactly what the controllers need to communicate with the views, and exactly what the views need to communicate with the controllers. BLL might be about business logic and might use classes that don't exactly match any ViewModel.

For example, a ViewModel can contain information about a customer and their company and products ordered by the customer in the last 3 months. It can also contain other data that will be used to create UI elements in the view: for example, a list of shipping methods. This data almost certainly comes from several different BLL classes and methods. The form of this data is focused on the communication between the view and the controller. BLL classes are focused on business logic and possibly database.

+4


source







All Articles