泛型函數,我有以下方法:簡化代碼
private JobCard PopulateObject(JobCard jc, DataRow dataRow)
{
PropertyInfo[] proplist = jc.GetType().GetProperties();
foreach (PropertyInfo propertyitem in proplist)
{
if (propertyitem.Name != "")
if (propertyitem.PropertyType.BaseType.Namespace == "System")
{
propertyitem.SetValue(jc, dataRow[propertyitem.Name], null);
}
else
{
string typename = propertyitem.ToString().Replace("Pss.Common.Mia.", "");
int i = typename.IndexOf("Base");
typename = typename.Substring(0, i);
Type type = propertyitem.PropertyType;
switch (typename)
{
case "Customer":
propertyitem.SetValue(jc, PopulateCustomerObject(propertyitem, dataRow, type), null);
break;
case "Meter":
propertyitem.SetValue(jc, PopulateMeterObject(propertyitem, dataRow, type), null);
break;
case "TimeSheet":
propertyitem.SetValue(jc, PopulateTimeSheetObject(propertyitem, dataRow, type), null);
break;
}
}
}
return jc;
}
以上方法調用這些:
private Customer PopulateCustomerObject(object o, DataRow dataRow, Type type)
{
IDataStorable instance = (IDataStorable)AppDomain.CurrentDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
PropertyInfo[] proplist = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
Customer c = new Customer();
Guid customerGuid = new Guid(dataRow["AddressId"].ToString());
string view = ReflectionHelper.GetAttribute<DBObjectRetrieveAttribute>(type).View;
string query = string.Format("select * from {0} where id = '{1}'", view, customerGuid);
c = DataAccess.Retriever.Retrieve<Customer>(query);
return c;
}
private Address PopulateAddressObject(object o, DataRow dataRow, Type type)
{
IDataStorable instance = (IDataStorable)AppDomain.CurrentDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
PropertyInfo[] proplist = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
Address a = new Address();
Guid AddressGuid = new Guid(dataRow["PhysicalAddressId"].ToString());
string view = ReflectionHelper.GetAttribute<DBObjectRetrieveAttribute>(type).View;
string query = string.Format("select * from {0} where id = '{1}'", view, AddressGuid);
a = DataAccess.Retriever.Retrieve<Address>(query);
return a;
}
private Meter PopulateMeterObject(object o, DataRow dataRow, Type type)
{
IDataStorable instance = (IDataStorable)AppDomain.CurrentDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
PropertyInfo[] proplist = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
Meter m = new Meter();
Guid meterGuid = new Guid(dataRow["MeterId"].ToString());
string view = ReflectionHelper.GetAttribute<DBObjectRetrieveAttribute>(type).View;
string query = string.Format("select * from {0} where id = '{1}'", view, meterGuid);
m = DataAccess.Retriever.Retrieve<Meter>(query);
return m;
}
,我可以看到將最好的1種泛型方法所取代,但如何?
我不明白如何與1條普通線路取代
Customer c = new Customer();
Address a = new Address();
Meter m = new Meter();
TimeSheet t = new TimeSheet();
,也
c = DataAccess.Retriever.Retrieve<Customer>(query);
a = DataAccess.Retriever.Retrieve<Address>(query);
m = DataAccess.Retriever.Retrieve<Meter>(query);
t = DataAccess.Retriever.Retrieve<TimeSheet>(query);
我不能改變Retriever.Retrieve。它被聲明爲
public static T Retrieve<T>(string query)
where T : IDataStorable
{
return Retrieve<T>(query, new IDbDataParameter[0], string.Empty);
}
請澄清一些事情;目前尚不清楚在「填充」方法中如何使用`proplist`或`o`;是'類型'客戶/地址等?爲什麼複雜的CreateInstanceAndUnwrap?另外 - 謹防SQL注入... – 2009-05-27 07:27:45
另請參見:propertyitem;你將它作爲`o`傳遞,然後(分別)調用propertyitem.ToString() - 我不認爲這是一個好主意......它試圖做什麼? – 2009-05-27 07:29:56
callisto 2009-05-27 08:03:39
早期的mornig,咖啡還沒有被踢過 - 但是這個列表是不必要的,從之前的這個問題的嘗試中複製並粘貼。因此CreateInstanceAndUnwrap也是多餘的。客戶,地址,計量表和時間表是類型。層次結構是:JobCard包含TimeSheet,Neter,Customer(它包含Address)SQL注入不是問題,因爲這些方法是通過經過驗證的WM5應用程序跨web服務調用的 –