我有以下方法,它们都返回不同的类型。我有四种这样的方法。 本着良好编程实践(DRY)的精神,应该在这里使用一些OOP技术,例如继承或接口,或者只是使用它。欢迎任何评论或代码示例。谢谢。
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;
}
}
答案 0 :(得分:13)
怎么样:
// 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);
}
}
}
请注意,我已添加using
个参数,以避免泄露连接。根据文档,您也不需要关闭流。
如果return null
是不可为空的值类型,则return default(T)
必须更改为T
;另一种方法是使用T
作为方法声明的一部分,将where T : class
限制为引用类型。
像这样使用:
MemberResponse members = FetchItem<MemberResponse>(request);
...
AttendeeResponse attendee = FetchItem<AttendeeResponse>(request);
答案 1 :(得分:1)
使用泛型?
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;
}
}
答案 2 :(得分:0)
您可以使用泛型:
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;
}
}
答案 3 :(得分:0)
它看起来像是一个很好的候选者。也许..?
static E GetPiece< E >(HttpWebRequest request) {
{
...
}
答案 4 :(得分:-1)
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;
}
}
答案 5 :(得分:-1)
您可以通过代理消除重复的代码。 例如: 如果我必须完成两个针对数据库的任务:
OleDbCommand oci = new OleDbCommand(queryString, connection);
oci.Connection.Open();
oci.CommandText = "delete * from firstTable";
oci.ExecuteNonQuery();
oci.Connection.Close();
OleDbCommand ocn = new OleDbCommand(queryString, connection);
ocn.Connection.Open();
ocn.CommandText = "select count(*) from secondTable";
int affected = convert.ToInt32(ocn.ExecuteScalar();
ocn.Connection.Close();
你可以看到我们这里有重复的代码。所以我需要像这样重构我的代码:
OleDbCommand myAccessCommand = null;
int count = -1;
OleDbWork(secondConnectionString, "SELECT COUNT(*) FROM secondTable",
out myAccessCommand,
() =>
{
count = Convert.ToInt32(myAccessCommand.ExecuteScalar());
});
OleDbWork(firstConnectionString, "delete * from firstTable",
out myAccessCommand,
() =>
{
myAccessCommand.ExecuteNonQuery();
}
我的OleDbWork方法如下所示:
internal bool OleDbWork(string connString, string command,
out OleDbCommand myAccessCommand, Action action)
{
OleDbConnection myAccessConn = null;
myAccessCommand = null;
try
{
myAccessConn = new OleDbConnection(connString);
}
catch (Exception)
{
MessageBox.Show("Cannot connect to database!");
return false;
}
try
{
myAccessCommand = new OleDbCommand(command, myAccessConn);
myAccessConn.Open();
action();
return true;
}
catch (Exception ex)
{
MessageBox.Show("Cannot retrieve data from database. \n{0}",
ex.Message);
return false;
}
finally
{
myAccessConn.Close();
}
}
我希望这会有所帮助