I have the following methods, that all return different types. I have four of such methods.
In the spirit of good programming practices(DRY), should be using some OOP techniques such as inheritance or interfaces here or just roll with it. Any comments or code examples are welcome. Thank you.
static AttendeeResponse GetAttendees(HttpWebRequest request)
{
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
try
{
XmlSerializer ser = new XmlSerializer(typeof(AttendeeResponse));
return (AttendeeResponse)ser.Deserialize(resp.GetResponseStream());
}
catch(Exception e)
{
error = e.InnerException.ToString();
return null;
}
}
static MemberResponse GetMembers(HttpWebRequest request)
{
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
try
{
XmlSerializer ser = new XmlSerializer(typeof(MemberResponse));
return (MemberResponse)ser.Deserialize(resp.GetResponseStream());
}
catch (Exception e)
{
error = e.InnerException.ToString();
return null;
}
}
How about:
Note that I’ve included a
usingstatment to avoid you leaking connections. You don’t need to close the stream as well, according to the docs.The
return nullhad to change toreturn default(T)in caseTis a non-nullable value type; an alternative would be to restrictTto be a reference type usingwhere T : classas part of the method declaration.Use it like this: