This is the third part of the solution for Papa Bob's Mega Challenge. In this part of the solution, we'll be setting up the Data Transfer Object to pass an order created in the Presentation layer all the way to the Persistence layer.
Because we'll be utilizing the OrderDTO class to pass an order from the Presentation layer, we'll need to modify its properties to match those of the Entity-generated Order class of the Persistence layer. To do this, navigate to 'PapaBobsEntities' in the Persistence layer, and open its Order.cs class:
Copy all of the properties from the Order class' definition. Then, navigate to OrderDTO.cs in PapaBobs.DTO. Paste the properties from the Order class here so that it looks like this:
The ultimate goal in this part of the solution is to pass an order from the Presentation layer to the database. This will happen when the orderButton_Click event is activated. Navigate to Default.aspx.cs and write the following code in the orderButton_Click to create a new instance of the OrderDTO class and then hard-code its properties:
Now that we have an orderDTO created within this layer, we need to modify the method calls within the other layers to expect an input parameter of DTO.OrderDTO. This allows for the Presentation layer's orderDTO to be passed through the different layers.
Now, in the OrderManager's CreateOrder() method, modify the call to the Persistence, passing in the orderDTO from the input parameter:
Finally, return to Default.aspx.cs and modify the call to the Domain layer's CreateOrder(), passing in the orderDTO:
Now that we've passed the orderDTO to the Persistence, we need to save it to the database. However, there is an issue: The orderDTO is of a different type than an Entity Order. What we'll need to do is modify the CreateOrder() method in the Persistence layer so that it calls a helper method to convert the order into the proper type:
This modification calls a method that, at this point, has not yet been created. Select the 'convertToEntity()' text and press Ctrl + . to generate a new method stub for that method:
This method will be responsible for creating a new Entity-type order and populating its properties with those of the orderDTO that was passed in to the method:
Notice that the method's return type is changed to Order (Entity-type order), which gives the CreateOrder() method the object it needs to store in the database.
Before we run the project and add the order to the Orders table, make sure to clear out the table of previous data so that the new order is the only order visible in the table. To do this, open the Server Explorer, navigate to Tables and select the Orders table. Right-click and select 'Show Table Data'. From here, select the row you want to delete, right-click on the gray arrow to the left of the entry and click delete:
Then, save and run your project, click on the Order button, and return to the database to to ensure that the order was saved properly.
This concludes this part of the solution to Papa Bob's Mega Challenge. We've created much of the core functionality needed for the layers to interact and pass information back and forth. In the next part, we'll create the Default page with its controls and styling.
Papa Bob's Mega Solution Part 3 - Passing an Order from the Presentation Layer