|
| Web News |
Blog-savvy French set for cyberbattle in 2007 poll Cyberspace is turning into a hotly contested arena in this year's French presidential elections, as candidates seek to harness the Internet's vast but volatile resources to give them a decisive edge. Frontrunners Segolene Royal and Nicolas Sarkozy issued their New Year's greetings on the...
TV production company agrees to buy its French... Endemol, the TV production company behind "Big Brother," agreed to buy its French unit from the Spanish company Telefónica for as much as €450 million. The deal could accelerate Telefónica's effort to sell Endemol...
MySpace extends European reach with French site Social networking powerhouse MySpace will launch its French site this week as part of a broader move into Europe. MySpace, which lets members look up friends' blogs and songs posted by their favorite artists...
Tiny Linux computer aims at terminal/kiosk users IT administrators who need to deploy and manage very small desktop or kiosk-style PCs may want to keep an eye on the development of Linutop. The gadget, about the size of a portable CD player, is an embedded...
IBM Wants To Help Small Businesses Bring Products... IBM rarely competes directly with Oracle and SAP in ERP applications. But that's changing on one front: product life-cycle management. With PLM's heavy emphasis on data transfer and data management, the technology plays to IBM strengths and gives it a toehold versus SAP...
|
 | |
|
01.11.07 ASP.NET: A Simple Factory Using Generics By
Mads Kristensen
For about a week one of my readers asked me to simplify a piece of his code.
The code was translating the different tags used by blog comments, such as [url][/url], [img][/img] etc. as we know from WordPress. It was one big method that processed the different types of tags before the comment was saved to a database.
I suggested a very simple factory design by using Generics and an interface. Then for each tag we would create a new class for handling only that single tag. The class had to implement the interface called IHtmlFilter. This is the interface:
As you can see, the UrlFilter class implements the IHtmlFilter member Execute(ref string html) which we will use later. We also created an ImageFilter class to handle the [img][/img] tags using roughly the same technique as the UrlFilter. So now we had the two classes, UrlFilter and ImageFilter.
Cost Effective Website and Network Monitoring IPCheck Server Monitor - Free Download |
|
Because they both implement the IHtmlFilter interface, we were able to treat them as being of same type and put them in a Generic collection. When we have an instance of the two classes in the collection, we can iterate the collection and call the Execute(ref string html) method on each of the classes. The html parameter passed by ref is the blog comment, that needs to be processed for the [img] and [url] tags. Here is how to add the classes to the collection:
The code fills the static collection _Filters with instances of the two classes that implements the IHtmlFilter interface. Now all we have to do to process the blog comment is to put the comment into the static method ApplyFilters(ref string html) below.
Continue readng this article.
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/
|
|