3

I have read this and was wondering about this.

My application contains 4 Layers

  1. Web Project / UI
  2. BLL
  3. DAL (contains EF)
  4. Entity Layer

I have placed VM in UI layer as of now and its a combination of different classes. something like this

    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; }
    }

I am confuse now that how I can send this data from UI to BLL and then DAL. I have read by automapper but does it handle this situations, if yes then how? As of now, I have decided to move VMs to Entity Layer which will be connected to all the three layers so that I can send and receive data in the same, 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 I can pass data as BLL does not have the definition of CompanyVMIndex which is a VM and in Web UI

Community
  • 1
  • 1
  • Make a separate Class Library for View Models. That would be complimenting to your architecture. Also will help in keeping all layers specific to what they do. – Dhrumil Jun 12 '15 at 06:43
  • what about placing the same in Entity Layer? – Mohammed Dawood Ansari Jun 12 '15 at 06:44
  • Well, you can. But that would mix two different things into one layer. If you keep the layer separate, you can provide references to other layers as per your need. – Dhrumil Jun 12 '15 at 06:46
  • that would mix two different things into one layer?? Not quite clear to me. Actually, I have created Entity layer for such situation; when I want to access something from UI, BLL and DAL, so all of them can have entity layer's reference, so that i can prevent accessing DAL from UI directly, UI has to go via BLL or Entity Layer. – Mohammed Dawood Ansari Jun 12 '15 at 06:51
  • See John's answer below. That is a good approach and something similar to what I said. – Dhrumil Jun 12 '15 at 06:59
  • the main question that how to pass data from UI to BLL is still unanswered. I have edited question for better understanding. – Mohammed Dawood Ansari Jun 12 '15 at 07:23
  • You act like there's something special about the different layers. They are composed of classes, which have methods. You pass the data by calling the methods. – John Saunders Jun 12 '15 at 07:29

1 Answers1

5

If you want to be "pure", then the ViewModel (or, in general, whatever model you send to the view) will never be seen by your BLL or any other layer. It will only ever be used to communicate between controllers and views.

When it comes time to retrieve data from the BLL or send data back to the BLL, other classes would be used. Data would be copied to and from the ViewModel classes.

This way, the ViewModel contains precisely what the controllers need in order to communicate with the views, and exactly what the views need to communicate back to controllers. The BLL can be about business logic, and may use classes which do not exactly correspond to any ViewModel.

For example, a ViewModel may contain information about a customer and his company, and about the products the customer has ordered in the past 3 months. It may also contain other data to be used to create user interface elements in the view: for instance, a list of shipping methods. This data almost certainly comes from several different BLL classes and methods. The shape of this data is oriented towards communication between the view and the controller. The BLL classes are oriented towards business logic and possibly the database.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Agreed, but now the question is, how can I pass data from UI to BLL. Do I need to create copy of the same classes in the BLL. – Mohammed Dawood Ansari Jun 12 '15 at 06:59
  • 1
    What are you asking? The BLL is just a bunch of classes, with data and methods. You pass data from the UI to the BLL by calling methods. – John Saunders Jun 12 '15 at 07:00
  • 1
    You do not pass the ViewModel to the BLL - ***ever***. Your ViewModel will contain some pieces of data that you may want to pass to the BLL. But you _never_ pass presentation-layer classes to the BLL. – John Saunders Jun 12 '15 at 07:24
  • then how I can pass the data what I got from the user? – Mohammed Dawood Ansari Jun 12 '15 at 07:26
  • 1
    I think we're not both speaking English. What if you get the number 1 from the user. It's stored in `MyViewModel.NumberFromUser`. Then you can pass it to the BLL by calling `BLL.DoSomethingWithNumber(myViewModel.NumberFromUser)`. – John Saunders Jun 12 '15 at 07:28
  • I have understood what u have said and I have shown the same code in my question as well, I got that how to send data from UI but when it comes to BLL, I would require same class to accept the data, isn't it? so as to accept the data we need same type of class which is nothing by VM in my case. Hope you understood the question. – Mohammed Dawood Ansari Jun 12 '15 at 10:05
  • You need to call the BLL using the BLL class. You need to copy data from the ViewModel to the type that the BLL is expecting to receive. There are tools to do this. In particular, look up "AutoMapper". – John Saunders Jun 12 '15 at 15:55
  • 1
    Even though the thread is old I want to add some sense to the discussion. Totally agree with what @John said. Your VM can have 5 fields. You pass 3 of them to a function in your BLL class and 2 to another function. How you pass is to use Automapper. For some reference see https://www.codeproject.com/Articles/639618/CRUD-Opearations-using-AutoMapper-in-an-MVC-Applic – aditya Dec 27 '16 at 11:04
  • @MohammedDawoodAnsari I should have been more clear. If your UI sends you user data in a ViewModel object, and if you want to pass it to BLL methods which use BLL objects, then you have to copy the data from the ViewModel object to the BLL objects. This will not necessarily be a one to one mapping. You could have a view that has an editable table of products, for instance. You'd have to call a BLL "Update" method for each modified product, copying from the ViewModel product objects to multiple BLL product objects, perhaps in a loop. – John Saunders Jul 26 '21 at 07:01