先来一个简单的接口
第一章中弄了一个通用类,现在加工一下
public class SAPConfig { public static RfcDestination Prd; static IDestinationConfiguration ID = new SapHelper.MyBackendConfig(); /// <summary> /// 这个信息的注册,因为只能注册一遍,多线程注册会报错 /// </summary> static SAPConfig() { RfcDestinationManager.RegisterDestinationConfiguration(ID); //登录 Prd = RfcDestinationManager.GetDestination("PA0_000"); } public void Logout() { RfcDestinationManager.UnregisterDestinationConfiguration(ID); } }
public List<T> Execute<T>(string[] columns, string tableName, string functionName, Dictionary<string, object> requestParam) where T : new() { //登录 RfcDestination prd = SAPConfig.Prd; List<T> dt = Execute<T>(prd, columns, tableName, functionName, requestParam); //退出登录 return dt; }
private List<T> Execute<T>(RfcDestination prd, string[] columns, string tableName, string functionName, Dictionary<string, object> requestParam) where T : new() { List<T> returnList = new List<T>(); RfcRepository repo = prd.Repository; IRfcFunction companyBapi = repo.CreateFunction(functionName); //调用函数名 //设置Import的参数 foreach (var item in requestParam) { companyBapi.SetValue(item.Key, item.Value); } companyBapi.Invoke(prd); //执行函数 IRfcTable table = companyBapi.GetTable(tableName); //执行函数之后的GetTable或许返回参数的表结构 for (int i = 0; i < table.RowCount; i++) { T newModel = new T(); foreach (string clmn in columns) { typeof(T).GetProperty(clmn).SetValue(newModel, table[i].GetString(clmn).ToString()); } returnList.Add(newModel); } prd = null; repo = null; return returnList; }
这里就是针对简单的接口参数进行一个反射,这样不用每一个接口写一个查询方法了
不过反射写的不严谨,因为我全是string类型,这里不报错,如果有其他类型的,转化一下类型
简单接口的定义是:输入参数就几个字段,没有表或结构
后面会有复杂接口的调用
SapHelper helper = new SapHelper(); string[] cloumns = new string[] { "SAKNR", "TXT50", "MCOD1" }; Dictionary<string, object> requestParams = new Dictionary<string, object>() { { "BUKRS","G000" } }; var list = helper.Execute<与返回参数一样的类型>(cloumns, "ZACCT", "ZFKB_ACCT_READ", requestParams);
public class 与返回参数一样的类型//这里就是接口的三个返回参数
{
public string SAKNR { get; set; }
public string TXT50 { get; set; }
public string MCOD1 { get; set; }
}
这里确实挺简单的,就不一行行代码解释了,
IRfcFunction companyBapi = repo.CreateFunction(functionName); //调用函数名
这句代码执行之后,就已经有了很多东西,把鼠标放上去, 将companyBapi.Metadata的值拿出来在notepad里面,将逗号换成换行,然后所有的输入参数和输出参数就都有了
我拿到的接口参数文档是不全的,所以需要比对一下比较好
到这一步,一个简单的接口就调用成功了,后面记录一下复杂的