architecturecontrollergrasp

What to do with user input when using the GRASP controller?


enter image description here

Let’s suppose a user wants to add a customer to the system. He fills a form with the customer information and then press a button. The click event is captured by a form object in the UI layer. Who is responsible for creating a new customer object with the information provided by the user, the form itself or the controller in the Business layer?

In the book “Applying UML and Patterns”, in the examples I’ve seen so far the user input is passed to the controller in the form or parameters, and then the controller creates the appropriate object. My doubt is because I was told that it’s preferable to pass data between layers using objects instead, and not a bunch of parameters.


Solution

  • This is a pretty broad question on which you are likely to receive mostly opions, not problem solutions. In my opinion (sic) it's clearly not the responsibility to create the object, but probably the job of a separate object (a factory most likely). The job of the controller is to dispatch work coming in to other services -- that's it's single responsibility.

    On the same ground, object creation in the form is out of the question anyway, but there is another reason not to create the object in the form: the customer object belongs to the business layer (domain), not to the presentation layer. This argument becomes invalid if you are talking about object creation for the purposes of transferring data between the different layers only, cf. DTO. In this case, it's would be fine if the UI layer creates a CustomerDTO and hands it over to the controller (the above discussion of responsibility for object creation applies here, too).