I have placed comments with my questions inline. The code supports making rest calls.
// (1) Is appending Base to the name of base classes useful?
public abstract class RestCallBase : IRestCall
{
// (2) Is there a good way to decide on the ordering/grouping of members?
// I have seen code that uses #region for this, but I feel like it's not
// pervasive.
public string Url { get; set; }
public string Method { get; set; }
public string PostData { get; set; }
public string ResponseText { get; set; }
// (3) How do you feel about using the same name for type and identifier
// in cases like this? I go back and forth because on one hand it feels
// cleaner than using underscore (_MyPrivateProperty) or
// camel cased (myPrivateProperty).
private HttpWebRequest HttpWebRequest { get; set; }
// (4) Is it clear that the target of the lone verb comprising the method
// name is the noun which is the name of the class? To me, it is
// redundant to say 'PrepareForRestCall'.
protected abstract void Prepare();
public IRestCall Go()
{
this.Prepare();
HttpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
// (5) Here a region is used in place of a comment, but I have not
// seen any other code that uses regions this way. My thinking is
// that it brings the code one step closer to becoming a private
// method but can stay like this until it actually needs to be called
// from multiple points in the logic.
#region Add post data to request if present.
if (!string.IsNullOrEmpty(PostData))
{
// (6) I changed this from 'sw' to 'writer' after code review.
// Would you have as well?
using(StreamWriter writer = new StreamWriter(HttpWebRequest.GetRequestStream()))
writer.Write(PostData); // (7) Would you use curly braces for a single statement? I opt to save two lines so I can see more code on the screen.
}
#endregion
using (HttpWebResponse response = HttpWebRequest.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
ResponseText = reader.ReadToEnd();
}
return this;
}
}