0

I am inheriting a legacy code. The legacy code has lot of web forms and each web form does not have much of functionality other than hosting a user control.

My question is -

  • Should we create a user control even if that control is not used in any other web form ?

  • Are there any hard benefits in creating user controls over web forms?

P.S - Web forms are .aspx files and User Controls are .ascx files.

1

There are two approaches I can see:

  • One may use user controls just like one uses objects to make primitive data types and basic types within the language or framework more explicit or to enclose logic within them.

    For example, in OOP, when you deal with percentage, one way is to use an int; a usually preferred way is to create Percentage class which will enclose the value, as well as its own logic, such as the limited range 0..100.

    In the same way, instead of putting an <input type="text" /> for a temperature in Fahrenheit, one can create Temperature.ascx which, besides showing an input field, controls the value entered by the user (for example the range -459.67..∞) and adds the unit. Later, when business requirements change and the form should deal with Celsius, only Temperature.ascx has to be changed.

    In this context, user controls may be a valuable tool for obtaining a clean architecture and limit code changes required by the changes in business requirements.

  • Another way is to create user controls for nearly every field, with no business logic inside, thinking that it may be useful in the future when the business requirements change.

    For example, the uncontrolled (aside the maximum length) field Phone number may be enclosed within PhoneNumber.ascx control, expecting that one day, the app will check if the phone number is valid.

    This is an extremely bad practice which violates KISS principle and should be avoided. If you encounter user controls which have no logic inside, remove them and put simple ASP.NET controls or fields instead.

  • the legacy code I had inherited, differs from the first point you have listed. In your example, I assume the web form has some more functionality besides hosting the temperature control. In the code I inherited the web form does nothing beyond hosting the user control. Will that also lead to clean architecture? – Siva Senthil Apr 21 '14 at 9:07
  • @SivaSenthil: then it's probably the second point of my answer, and such user controls should be removed in favor of ordinary controls. – Arseni Mourzenko Apr 21 '14 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.