先來一個簡單的接口
第一章中弄了一個通用類,現在加工一下
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里面,將逗號換成換行,然后所有的輸入參數和輸出參數就都有了
我拿到的接口參數文檔是不全的,所以需要比對一下比較好
到這一步,一個簡單的接口就調用成功了,后面記錄一下復雜的