Click here to Skip to main content
15,887,267 members
Home / Discussions / C#
   

C#

 
GeneralRe: Cleaning up Arrays in C# Pin
Jeff Varszegi3-Nov-03 5:47
professionalJeff Varszegi3-Nov-03 5:47 
GeneralRe: Cleaning up Arrays in C# Pin
Paul Evans3-Nov-03 6:11
Paul Evans3-Nov-03 6:11 
GeneralRe: Cleaning up Arrays in C# Pin
MrEyes4-Nov-03 5:35
MrEyes4-Nov-03 5:35 
GeneralRe: Cleaning up Arrays in C# Pin
Paul Evans5-Nov-03 4:50
Paul Evans5-Nov-03 4:50 
GeneralDynamic / Run-time casting Pin
3-Nov-03 3:12
suss3-Nov-03 3:12 
GeneralRe: Dynamic / Run-time casting Pin
Paul Evans3-Nov-03 3:34
Paul Evans3-Nov-03 3:34 
GeneralRe: Dynamic / Run-time casting Pin
in10se3-Nov-03 4:24
in10se3-Nov-03 4:24 
GeneralRe: Dynamic / Run-time casting Pin
CBoland3-Nov-03 9:00
CBoland3-Nov-03 9:00 
I did something like this on a project. My solution was a variation of the GoF Bridge design pattern. The idea is to separate the abstraction (the base WebDocument class) from its implementation (child classes of WebDocument).

1. Make WebDocument an abstract class, so it can't be instantiated. Add a method that child classes must override that will be called to process themselves (i.e. Process). Create a static factory method on WebDocument to instantiate an the appropriate handler class. You'll need to pass some information into the method so the class can decide which to create (the HTTP header, etc.).

public abtract class WebDocument
{
protected HttpRequest Request;
public static WebDocument CreateInstance(HttpRequest r)
{
switch r.ContentType
{
case "text/html":
return new HtmlDocument(r)
break;
...
}
}
public abstract void Process()
}

2. Create a class for each document type you need to handle (PDF, Word, etc.) that inherits from WebDocument. Create something like UnhandledDocument to process documents that you don't currently support.

public class HtmlDocument : WebDocument
{
public HtmlDocument(HttpRequest r)
{
this.Request = r;
}
public override void Process()
{
// do something with this.Request
}
}

3. Write client code something like this:

HttpRequest req = HttpContext.Current.Request;
WebDocument d = WebDocument.CreateInstance(req);
d.Process();


Can you see how the abstraction (WebDocument) is separated from an implementation (HtmlDocument)? Supporting new document types is as easy as creating the implementation class and adding it into CreateInstance, and will affect no other code. The client doesn't know or need to know the instance type. All it is responsible for is getting an instance of WebDocument to process a request.

Hope this helps. It certainly helped me!

GeneralRe: Dynamic / Run-time casting Pin
in10se3-Nov-03 10:56
in10se3-Nov-03 10:56 
GeneralRe: Dynamic / Run-time casting Pin
Paul Evans9-Nov-03 12:02
Paul Evans9-Nov-03 12:02 
GeneralNewbie Socket Question Pin
Joel Holdsworth3-Nov-03 2:53
Joel Holdsworth3-Nov-03 2:53 
GeneralRe: Newbie Socket Question Pin
Eric Gunnerson (msft)3-Nov-03 10:04
Eric Gunnerson (msft)3-Nov-03 10:04 
GeneralXmlUrlResolver Pin
Member 6652453-Nov-03 2:42
Member 6652453-Nov-03 2:42 
GeneralRe: XmlUrlResolver Pin
Paul Evans3-Nov-03 3:36
Paul Evans3-Nov-03 3:36 
GeneralRe: XmlUrlResolver Pin
Member 6652453-Nov-03 3:49
Member 6652453-Nov-03 3:49 
Generalprocess index problem Pin
Anonymous3-Nov-03 1:45
Anonymous3-Nov-03 1:45 
GeneralRe: process index problem Pin
Paul Evans3-Nov-03 3:37
Paul Evans3-Nov-03 3:37 
GeneralThreading not working Pin
Member 6797283-Nov-03 1:26
Member 6797283-Nov-03 1:26 
GeneralRe: Threading not working Pin
Paul Evans3-Nov-03 3:39
Paul Evans3-Nov-03 3:39 
GeneralRe: Threading not working Pin
Paul Evans3-Nov-03 3:48
Paul Evans3-Nov-03 3:48 
GeneralRe: Threading not working Pin
Member 6797283-Nov-03 17:39
Member 6797283-Nov-03 17:39 
GeneralUser Controls in C# Pin
Den2Fly3-Nov-03 0:51
Den2Fly3-Nov-03 0:51 
GeneralRe: User Controls in C# Pin
Paul Evans3-Nov-03 3:43
Paul Evans3-Nov-03 3:43 
GeneralRe: User Controls in C# Pin
Den2Fly3-Nov-03 4:06
Den2Fly3-Nov-03 4:06 
GeneralTextBox Pin
Jose Vicente3-Nov-03 0:46
Jose Vicente3-Nov-03 0:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.