Ich habe die folgende Methode:generische Funktion für eine vereinfachte Code
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;
}
Das obige Verfahren nennt diese:
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;
}
, die ich sehen kann, am besten durch ein gattungsgemäßes Verfahren würde ersetzt werden, aber wie?
Ich sehe nicht, wie
Customer c = new Customer();
Address a = new Address();
Meter m = new Meter();
TimeSheet t = new TimeSheet();
mit 1 generic Linie zu ersetzen, und auch
c = DataAccess.Retriever.Retrieve<Customer>(query);
a = DataAccess.Retriever.Retrieve<Address>(query);
m = DataAccess.Retriever.Retrieve<Meter>(query);
t = DataAccess.Retriever.Retrieve<TimeSheet>(query);
Ich kann nicht Retriever.Retrieve ändern. Es wird erklärt, wie
public static T Retrieve<T>(string query)
where T : IDataStorable
{
return Retrieve<T>(query, new IDbDataParameter[0], string.Empty);
}
Bitte klären ein paar Dinge; Es ist nicht klar, wie (wenn?) Sie "proplist" oder "o" in den "Populate" -Methoden verwenden; ist 'Typ' Kunde/Adresse usw.? Warum die komplizierte CreateInstanceAndUnwrap? Auch - Vorsicht SQL Injection ... –
Auch: propertyitem; du gibst das als 'o' ein und (separat) rufst propertyitem.ToString() auf - ich glaube nicht, dass beides eine gute Idee ist ... was versucht es zu tun? –