Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid duplicating logic on two similar WinForms?

I have two forms, form A and form B. These forms must differ in appearance, but they share a lot of logic. The problem is that this logic is tied to the appearance (validation on button click, events being fired, etc.). For example, I have a name field, and when the save button is pressed, I need to fire an event which causes the parent form to validate the record name to avoid duplicates. Both forms need this logic, but their save buttons are in different places, and the tooltip that is shown when an error occurs also needs to appear in a different place. This is just one example, but does anyone know of a way that I can avoid copying and pasting code here? Perhaps I am missing something obvious...

like image 704
Ed S. Avatar asked Nov 30 '22 13:11

Ed S.


1 Answers

You could create an object with data that is represented in both forms, and put validation logic in that object. The presentation layer should populate that object with the entered data, ask the object to validate itself, and then handle validation errors in a form-specific way.

like image 126
dnord Avatar answered Dec 05 '22 06:12

dnord