I had to convert one set of data to another… and it produced some very gnarly code. So what should we do with code like this?
I have a Model called “Customization” which stores details that users provide in order to customize a product. Once that product is paid for and ready to go, the Customization needs to be stored in another model called “Process” in preparation for some time consuming processes behind the scenes.
This conversion from Customization to Process requires knowledge of the the customization, the Product that was customized and all its various relationships, and the format that all this information needs to be reworked into before being saved by the Process model.
I could just give the Process model the logic to convert a Customization to a Process but that would have junked up the model, would require that model to know about many other models… quickly turning a maintainable project into something strung together and dependent on each other. Like House of Cards but with far less Spacey.
I created an adapter class called CustomizationToProcessAdapter, pass it the info it needs, do all the ugly stuff away from prying eyes, and then pass the results back to be saved to Process.
Beautify. Well the code isn’t beautiful… it is quite ugly. But it is contained in something that can be tested and refactored all by itself without breaking anything.