|
| Web News |
E*Trade tests global trading E*Trade Financial, an online bank and brokerage, has launched a pilot platform that offers U.S. customers electronic access to foreign stocks and currencies in international markets. The platform, which will be widely available in the second quarter, allows E*Trade's...
'Madame Wikipedia' runs web giant from village HQ Far from the power-broking of Silicon Valley, the new boss of the Internet giant Wikipedia is a soft-spoken French mother of three, who runs the global success story from a home office in a village in central France.
Yahoo Offers Mobile Display Ads In 19 Countries Yahoo is now offering mobile display advertising though its Yahoo Mobile Web service in 19 countries, extending the graphic ad platform for phones that it launched in the United States late last year. Yahoo is serving mobile phone banner ads from the likes of Hilton's Embassy...
Electronics execs seek reform on EU copyright tax Top electronics firms have asked the European Commission to discuss why the European Union executive body abruptly delayed reform of an "excessive and unjustified" copyright tax after lobbying by France.
Famed ID thief to speak at RSA Europe He posed as a doctor, a pilot and a lawyer without any training and got away with it. But he'll appear simply as himself to keynote an upcoming security conference. Frank Abagnale, whose life story was depicted in the film "Catch Me If You Can", has signed up to keynote the...
Paris unlocks open source for kids To help make kids aware of alternatives to proprietary software, the Ile-de-France (greater Paris) region will be giving 175,000 schoolchildren and apprentices a USB-key loaded with open-source software. The keys, which will contain a "portable office", will be given to 130,000...
|
 |
|
02.22.07 Creating Reusable User Controls In ASP.NET By
Mads Kristensen
In almost every web project of any size, you would probably use a lot of user controls to separate the content and UI logic.
Many of the user controls are used only at one place in the solution while others are used by various pages and other user controls.
The ones that are used throughout the website needs to be generic and loosely coupled from the context in which they are used. If not, they would contain a lot of code only to cater for the needs of certain pages or other controls in which they are embedded. This is bad.
The rules of object oriented programming apply to ASP.NET just as much as for code libraries, which include loose coupling, encapsulation, high cohesion and polymorphism. This is easily forgotten due to the apparent differences between classic programming and ASP.NET development.
Loose coupling
A user control should not contain any context specific code, but only do the one thing that it was designed to. If the control is called AddComments.ascx it should do nothing more than add comments. Look at the name and use it as a guide for what it should do, and equally as important, what not to do.
By making it loosely coupled you gain a lot of flexibility and reusability for the user control. You could even use it in other web projects because it is not dependant on the context in which it
lives.
Encapsulation
The inner workings of a user control should be kept invisible by the user of that control. That is does simply by making the various members private. The AddComments.ascx control only has to expose two properties and that is Name and Comment. Those properties should be made public and everything else should be kept private.
Try a Better Way Today.
Try WebEx MeetMeNow - Click
Here |
|
If the user control has some kind of events like a button click event, that button should not be public so the page can listen for the click event. Instead, you should create a public event that you raise internally from the buttons click event handler. By exposing the button you make it harder to understand and use the control. Here is how to add events.
High cohesion
Keep the control focused on the single task it was built to solve. All the methods should relate to each other in some way, so you don't end up having generic code in them.
In the AddComments.ascx control you might have some methods that clean the entered comments so it doesn't contain any JavaScript and other nasty things. You should consider moving those methods out of the user control, because they could represent a core functionality that isn't that much related to adding comments.
If you don't, you could end up with a huge class that does a lot of other things than adding a comment. That damages maintainability and removes focus of the real problem that it solves.
Polymorphism
If you have more than one user control that exposes some of the same properties because they are used in much the same way, you should consider implementing a common interface. That makes them more flexible for the page to use, because it can treat them as they were of same type.
If they share more than just public properties, then a base class is the way to go. The base class can provide common functionality to be used within the user control, so it doesn't have to be implemented into every one of them.
About the Author: Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.
http://www.madskristensen.dk/
|
|