我以前见过这种模式/方法,我正在尝试重新创建它以使我现有的一些代码更有效率。
用例: 从源系统中检索复杂对象。客户端只会使用一部分信息,因此我们必须将这个复杂的对象“映射”到一个简单的 POCO 以进行 JSON 序列化;此外,在这种映射方法中,还完成了一些其他数据格式化。首先,我们将复杂对象传递给一个通用方法,该方法进行一些基本处理
// Generic Method, Entry Point for mapping
static void GenericEntry<T, TK>(string userid, string environment, DBContext context) {
.... // do stuff with userid and environment to set a context
.... // query results, which return a complex object of Type TK
// Here is where I would like to use an Action delegate to call the appropriate map
// method.. there could hundreds of objects that map and process down to a POCO,
// Currently, this logic is using reflection to find the appropriate method with
// the appropriate signature... something like:
Type functionType = typeof(DTOFunctions);
var methods = functionType.GetMethods(BindingFlags.Public | BindingFlags.Static);
var mi = methods.FirstOrDefault(x => x.Name == "MapObject" &&
x.ReturnType == typeof(T));
if (mi == null) throw new ArgumentException(string.Format("Unable to find method MapObject for {0}", typeof(TK).Name));
var resultList = new ArrayList();
foreach (var row in results)
{
var poco = mi.Invoke(functionType, new object[] { row });
resultList.Add(poco);
}
if (resultCount == -1) resultCount = resultList.Count;
return SerializeDTO(resultList, ResponseDataTypes.JSON, resultCount);
// THERE HAS TO BE A BETTER WAY STACKOVERFLOW! HALP!
}
public Class DTOFunctions {
// Mapping Method from Complex to Simple object
static SimplePOCO_A MapObject(ComplexObject_A cmplx){
var poco = new SimplePOCO_A();
.... // mapping from cmplx field to SimplePOCO field
}
static SimplePOCO_B MapObject(ComplexObject_B cmplx) {
var poco = new SimplePOCO_B();
.... // mapping from cmplx field to SimplePOCO fiel
}
}