1

I'm working on a project where I need to extend WebClient for a custom implementation, MyWebClient.

I also need to make sure that MyWebClient is the only version that's implemented by any developer on the project and no one uses the base WebClient class.

Is it possible to prevent the usage of WebClient, considering it's not code that I have access to?

remio
  • 1,242
  • 2
  • 15
  • 36
HotN
  • 4,216
  • 3
  • 40
  • 51
  • 11
    This seems smelly. Why do you need to prevent usage of `WebClient`? – Etienne de Martel Mar 19 '12 at 20:53
  • Why make a base class and child class if there is only ever one class to extend the base? Move the code from the base class to the child class and be done with it. – Servy Mar 19 '12 at 20:54
  • 1
    @Servy I assume he means WebClient, in System.Net, is his base class. He's extending that and wants to ensure that users only ever instantiate his extended version. – Rob Bell Mar 19 '12 at 20:57
  • 1
    What about telling all the developers. _"Use the new `MyWebClient` class only"_? – gdoron Mar 19 '12 at 21:00
  • Why is this question getting downvoted? Because it's poor design? – Marlon Mar 19 '12 at 21:01
  • My specific need is to ensure the User-Agent header is set to a specific value throughout the product. My hope was there would be a way to enforce it without simply saying "hey everyone, remember to use this subclass". – HotN Mar 19 '12 at 21:08

4 Answers4

2

Depending on the features of WebClient you need, you could consider implementing MyWebClient as proxy that exposes only the methods you allow to be used from a WebClient member.

Example:

public class MyWebClient
{
    private WebClient HiddenWebClient {get; set;}

    // proxy sample
    public void DownloadFile(string address, string fileName)
    {
        HiddenWebClient.DownloadFile(address, fileName);
    }

   // other proxy methods & your specific implementation come here.

}

After, you will need a dedicated ctor and a maybe a factory to instantiate MyWebClient properly.

This won't prevent your developers from using WebClient which would be far too difficult (see @gdoron suggestion, for instance), but will help avoiding its usage by mistake.

Edit:

From your last comment, I think that all you need is a Factory that will set the User Agent for all your WebClient instances.

Then, depending on your organization, you will need a strong communication about its usage (and maybe a search tool to look for new WebClient() in your projects).

public static class WebClientFactory
{
    public static WebClient Create()
    {
        WebClient result = new WebClient();
        result.Headers.Add("My Fancy User Agent");
        return result;
    }
}
remio
  • 1,242
  • 2
  • 15
  • 36
  • I was hoping that some clever (and easy) mechanism for strictly enforcing this existed that I didn't know about. I guess I'm not surprised it doesn't. Ultimately, it seems like it really does come down to team communication. I like the extra options on keeping it more obvious for everyone on the team though! – HotN Mar 20 '12 at 21:02
1

The best way to do this is to code review all the code and make sure that WebClient isn't being used. Another option is to schedule a nightly task and pull from the source control and search through each file in the project to see if they use WebClient instead of MyWebClient (might be easier to do using Roslyn). Bottom line is that this isn't a code issue (or shouldn't be) it should be a training issue.

Jetti
  • 2,418
  • 1
  • 17
  • 25
0

If you are using ReSharper, you could use the "Search with pattern" feature, and set it up to offer to automatically convert the WebClient code to your MyWebClient just by pressing the magical ALT+Enter shortcut!

See: http://www.jetbrains.com/resharper/webhelp/Reference__Search_with_Pattern.html

Simon
  • 5,373
  • 1
  • 34
  • 46
0

If you really need it, (which I seriously doubt it)The only way you can do it is with reflection.

When the application starts scan for the code of all the methods and classes in the Assembly, and check that they don't use the forbidden WebClient class.

I guess you won't do it...

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • 1
    Ugh, if that's the only option, you're right. I'd prefer just to hope that all the developers remember to implement their code correctly than to do it at runtime. – HotN Mar 19 '12 at 21:10