开发者

Best practice, create interface of WebRequest

If I were to create an interface of Sy开发者_运维百科stem.net.WebRequest, what's the best way to do that?


To David's point, you first need to determine what it is that you want to do with the interface before you can decide what members it needs. If you want an interface for unit testing, I would recommend a separate approach. Take a look at the answer with the most votes to this question.

However, to answer your question strictly as asked, since you can't modify the WebRequest class, you'd first want to subclass it as so:

public class MyWebRequest : WebRequest, IMyWebRequest
{
}

You could then add all of the public members exposed by WebRequest to IMyWebRequest as so (remove members that you don't want exposed):

public interface IMyWebRequest
{
    Stream GetRequestStream();
    WebResponse GetResponse();
    IAsyncResult BeginGetResponse(AsyncCallback callback, object state);
    WebResponse EndGetResponse(IAsyncResult asyncResult);
    IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state);
    Stream EndGetRequestStream(IAsyncResult asyncResult);
    void Abort();
    RequestCachePolicy CachePolicy { get; set; }
    string Method { get; set; }
    Uri RequestUri { get; }
    string ConnectionGroupName { get; set; }
    WebHeaderCollection Headers { get; set; }
    long ContentLength { get; set; }
    string ContentType { get; set; }
    ICredentials Credentials { get; set; }
    bool UseDefaultCredentials { get; set; }
    IWebProxy Proxy { get; set; }
    bool PreAuthenticate { get; set; }
    int Timeout { get; set; }
    AuthenticationLevel AuthenticationLevel { get; set; }
    TokenImpersonationLevel ImpersonationLevel { get; set; }
    object GetLifetimeService();
    object InitializeLifetimeService();
    ObjRef CreateObjRef(Type requestedType);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜