我创建了这个通用方法:
/// <summary>
/// Executes a list of items
/// </summary>
/// <param name="procedureName">The name of the procedure</param>
/// <param name="parameters">List of oracle parameters</param>
/// <param name="limit">The limit of the results to return</param>
/// <param name="mapper">The oracle mapper method</param>
/// <returns></returns>
protected async Task<IEnumerable<T>> ListAsync<T>(string procedureName, IList<OracleParameter> parameters, int limit, Func<IDataReader, T> mapper)
{
// Create our list
var models = new List<T>();
var counter = 0;
// Get our reader and return our model
using (var reader = await _oracleUnitOfWork.ExecuteReaderAsync(procedureName, parameters))
{
// While we have rows and our counter
while (reader.Read() && ((counter < limit || limit == 0) || limit == 0))
{
// If we have a limit, increase our counter (this is first to stop 0 based index messing the amount of objects to return)
if (limit > 0)
counter++;
// Get our item from the database
models.Add(mapper(reader));
}
}
// Return our list
return models;
}
我可以这样调用:
return await ListAsync("IBPA_MANFCOMPLAINT.complaintSearch", parameters, yield, OracleMapper.Complaint.Map);
但我想稍微延伸一下。 Mapper只使用 IDataReader 将列值分配给模型属性。 对于股票而不是使用它:
return await ListAsync("ibpa_stock.readRelatedLotstoLot", parameters, 0, OracleMapper.Stock.Map);
我想做一个get请求,有点像这样:
return await ListAsync("ibpa_stock.readRelatedLotstoLot", parameters, 0, async (reader) => await GetAsync(reader["lotno"].ToString()));
我可以做些什么来保持干燥,但同时允许这种调用?
答案 0 :(得分:1)
您需要对方法进行两次重载,一次接受Func<IDataReader, T>
,另一次接受Func<IDataReader, Task<T>>
。第二个在调用方法时需要await
方法调用,前者不会。