public static List <T> RFCTableToGenericList <T>(IRfcTable rfcTable, Func <Dictionary <string, object>, T> mapper) { List <T> results = new List <T>(); foreach (var item in rfcTable.ToListOfDictionaries()) { results.Add(mapper(item)); } return(results); }
/// <summary> /// Delegate method responsible of building up the RFC request and map the response into a list /// of elements by using the provided mopping function. /// </summary> /// <typeparam name="K">Expected Type of elements according to the provided mapper parameter</typeparam> /// <param name="rfcName">Name of the RFC</param> /// <param name="parameters">Dictionary with the required parameters to invoke the RFC</param> /// <param name="mapper">Function delegate responsible of mapping table structures into list of elements of type T.</param> /// <returns>Dictionary that stores</returns> private Dictionary <string, object> RetrieveResponseData <K>(string rfcName, Dictionary <string, object> parameters = null, Func <string, Func <Dictionary <string, object>, K> > mapper = null) { SOFTTEK.SAP.Integration.SAPConnection sapConnector = new SAP.Integration.SAPConnection(); var sapDestination = sapConnector.LoadDestination(); var sapDestinationRepository = sapDestination.Repository; var sapRFC = sapDestinationRepository.CreateFunction(rfcName); /// Add Input Parameters if (null != parameters && parameters.Keys != null) { parameters.Keys.ToList().ForEach(k => sapRFC.SetValue(k, parameters[k])); } /// Invoke RFC for data retrieval sapRFC.Invoke(sapDestination); var rfcMetadata = sapRFC.Metadata; Dictionary <string, object> rfcParameters = new Dictionary <string, object>(); // Retrieve data from response for (int i = 0; i < rfcMetadata.ParameterCount; i++) { if (global::SAP.Middleware.Connector.RfcDirection.IMPORT.Equals(rfcMetadata[i].Direction)) { rfcParameters[rfcMetadata[i].Name] = new { Direction = kParameterInputDirection, Type = SAP.Integration.SAPData.GetDataType(rfcMetadata[i].DataType), Value = sapRFC.GetValue(rfcMetadata[i].Name) }; } else if (global::SAP.Middleware.Connector.RfcDirection.EXPORT.Equals(rfcMetadata[i].Direction)) { rfcParameters[rfcMetadata[i].Name] = new { Direction = kParameterOutputDirection, Type = SAP.Integration.SAPData.GetDataType(rfcMetadata[i].DataType), Value = sapRFC.GetValue(rfcMetadata[i].Name) }; } else if (global::SAP.Middleware.Connector.RfcDirection.TABLES.Equals(rfcMetadata[i].Direction)) { IRfcTable rfcTable = (IRfcTable)sapRFC.GetValue(rfcMetadata[i].Name); if (rfcTable != default(IRfcTable)) { if (mapper != null && mapper(rfcMetadata[i].Name) != null) { rfcParameters[rfcMetadata[i].Name] = new { Direction = kParameterOutputDirection, Type = typeof(List <K>), Value = SAP.Integration.SAPData.RFCTableToGenericList(rfcTable, mapper(rfcMetadata[i].Name)) }; } else { rfcParameters[rfcMetadata[i].Name] = new { Direction = kParameterOutputDirection, Type = typeof(Dictionary <string, object>), Value = rfcTable.ToListOfDictionaries() }; } } } } return(rfcParameters); }