How to refactor old C# Windows Forms application implementing DRY rule?

104 Views Asked by At

I am working on an old C# Windows Forms application code base. There are many forms with same form controls, needing the same logic for validation and some other tasks.

Consider the following code:

var patientMobileData = new PatientData
{
    PatientID = patient.SickID,
    PatientMobileNumber = txbPatientMobileNumber.Text,
    InsuranceOrganization = Convert.ToInt32(txbBaseInsuranceID.Text)
};

bool isMobileNumberValid = AppointmentInputsChecker
    .CheckPatinetMobileNumber(patientMobileData, out message);

if (!isMobileNumberValid)
{
    txbPatientMobileNumber.Focus();
    return false;
}

In this code segment, I validate if the mobile number is required based on insurance organization, and if the mobile number is not duplicate. I refactored this code so all forms use CheckPatinetMobileNumber method to validate mobile number.

But this segment has to be repeated in all forms (12 forms). How can I made it even simpler, so for example a parent form will handle all this logic, and the other forms inherit from that?

I created a parent form implementing all this logic that is "NOT" dependent on form-controls. But as you can see, there are controls related to each form, like txbPatientMobileNumber. So I removed them from each from designer and added to the parent form. In other words, I removed this line of code from form_1.designer.cs:

internal Ema.CustomeTextBox.EmaTextBox txbPatientMobileNumber;

And added the txbPatientMobileNumber to the parent form class (not in designer, but inside the class before declaring the parent class constructor):

public Ema.CustomeTextBox.EmaTextBox txbPatientMobileNumber;

I didn't noticed any problem until now; but as each control has some event, like onTextChange, This made problem for me since I removed the control definition from each form designer.

0

There are 0 best solutions below