tags:

views:

196

answers:

6

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;
        }

    }
A: 

Considering that the only difference in the method body is the cast, I'd say you should just pass the desired return type as a parameter, eliminating the duplicate methods.

Matt Ball
+12  A: 

How about:

// TODO: Improve the name :)
static T FetchItem<T>(HttpWebRequest request)
{
    using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
    {
        try
        {
            XmlSerializer ser = new XmlSerializer(typeof(T));
            return (T) ser.Deserialize(resp.GetResponseStream());
        }
        catch (Exception e)
        {
            error = e.InnerException.ToString();
            return default(T);
        }
    }
}

Note that I've included a using statment to avoid you leaking connections. You don't need to close the stream as well, according to the docs.

The return null had to change to return default(T) in case T is a non-nullable value type; an alternative would be to restrict T to be a reference type using where T : class as part of the method declaration.

Use it like this:

MemberResponse members = FetchItem<MemberResponse>(request);
...
AttendeeResponse attendee = FetchItem<AttendeeResponse>(request);
Jon Skeet
A: 

You could use Generics:

static T GetMembers<T>(HttpWebRequest request)
{
    HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

    try
    {
        XmlSerializer ser = new XmlSerializer(typeof(T));
        return (T)ser.Deserialize(resp.GetResponseStream());
    }
    catch (Exception e)
    {
        error = e.InnerException.ToString();
        return null;
    }

}
Matt Warren
+1  A: 

Use generics?

static T GetResponse<T>(HttpWebRequest request)
{
    HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

    try
    {
        XmlSerializer ser = new XmlSerializer(typeof(T));
        return (T)ser.Deserialize(resp.GetResponseStream());
     }
    catch(Exception e)
    {
        error =  e.InnerException.ToString();
        return null;
    }

}
Dave
A: 

It looks like a good candidate for generics. Perhaps..?

static E GetPiece< E >(HttpWebRequest request)    {
{
   ...  
}
Nestor
A: 

MemberResponse membResp = (MemberResponse )StaticClassName.SerializeIt(request);

static Object SerializeIt(HttpWebRequest request)
    {
        HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

        try
        {
            XmlSerializer ser = new XmlSerializer(typeof(MemberResponse));
            return (Object)ser.Deserialize(resp.GetResponseStream());
        }
        catch (Exception e)
        {
            error = e.InnerException.ToString();
            return null;
        }

    }
Kieran
That's going to use typeof(MemberResponse) even when fetching an AttendeeResponse...
Jon Skeet