This is the fifth part of the solution for Papa Bob's Mega Challenge. In this part, we are going to create enumerations to represent different properties being passed through the solution.
In order to avoid the use of "magic strings" in our code, as well as to make the project more easily modified in the future, we'll convert several of the Order properties from ints to enums. Specifically, the Crust, Size and PaymentType integers. If you recall from previous lessons, enums have an implicit zero-based integer value by which they're referenced. Because of this, we can take an integer value, such as Crust, and match it with an Enum.
To begin, add a new folder called 'Enums' to the 'PapaBobs.DTO' by selecting it in the Server Explorer and clicking Add > New Folder:
Next, add three new classes with the following names to this folder by right-clicking and selecting: Add > New Item:
Next, open the CrustType class and change it from a class to a public enum. Populate it with the following types:
These sizes will be implicitly assigned an integer value. Therefore, this declaration is equivalent to the following assignment:
For the sake of simplicity, we will leave these assignments implicit in this project. However, if for some reason in the future they needed to be overridden (such as the case of adding a stuffed crust), the code only needs to be changed in the enum, not in every reference throughout the program.
Next, go to the other two classes and make the following changes to change them to enums:
Now that the enums have been created, the appropriate properties can be assigned to them from within the Entity Data Model. Navigate to PapaBobsEntities within the Persistence layer to begin. Next, right-click on the Size property in the Order diagram and select 'Convert to Enum':
In the resulting dialog, set the Enum Type Name to 'SizeType'. Check the box at the bottom that says 'reference external type' and reference the SizeType Enum in the DTO class:
What the compiler is doing behind the scenes is worth noting. This Order diagram is derived of information actually stored in the Order class that was generated from our Orders table. When a property, such as this enum conversion, is changed in the diagram, it is reflected in that Order class:
Next, make the same conversion to the two other properties, referencing the appropriate enum:
Now, save all the projects in your solution to commit these changes. A dialog window may appear stating that external changes have been made to your properties, and asks if you'd like to reload them. Select 'Yes to All'.
These changes need to be reflected also in the OrderDTO class. Copy and paste the updated properties of the Order class into the OrderDTO class, replacing its previous properties:
Return now to the Default.aspx.cs page where we create the orderDTO. Notice that, in our hard-coded test, the Size, Crust and PaymentType properties are still set to integer values:
The problem with this is that the OrderDTO is now expecting an enum value to be supplied for these values, causing the compiler to give the following error:
We can resolve this issue by substituting enum values for these integers:
Save and run the project, then click on the Order button to see that the results are still saved successfully into the database:
This concludes this part of the solution for the challenge. In the next part, we'll take the user-supplied values and create an order using that information.
Papa Bob's Mega Solution Part 5 - Adding Enums