2016-08-02 47 views
1

我試圖改變的方法,使之異步和使用小巧玲瓏 這是一個repository類的當前方法:無法轉換使用toList()在異步方法

... 
public class ClientsRepository : BaseRepository 
    { 
     public ClientsRepository(string connectionString): base (connectionString){ } 
... 

    public List<ClientSummaryModel> GetClientSummaryCredit(string id) 
     { 
      try 
      { 
       connection(); 
       con.Open(); 
       con.ChangeDatabase("PAQA"); 
       IList<ClientSummaryModel> SummaryClientList= SqlMapper.Query<ClientSummaryModel>(con, "dbo.SP_GET_CLIENT_SUMMARY '" + id + "','0','CREDIT'", CommandType.StoredProcedure).ToList(); 
       return SummaryClientList.ToList(); 

      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
      finally 
      { 
       con.Close(); 
      } 
     } 

我想使其異步這樣的:

public async Task<List<ClientSummaryModel>> GetClientSummaryCredit(string id) 
    { 
     return await WithConnection(async c => 
     { 
      c.ChangeDatabase("PAQA"); 
      IList<ClientSummaryModel> SummaryClientList= await c.QueryAsync<ClientSummaryModel>("dbo.SP_GET_CLIENT_SUMMARY '" + id + "','0','CREDIT'", CommandType.StoredProcedure).ToList(); 
      return SummaryClientList.ToList(); 
     }); 
    } 

不幸的是,我得到以下錯誤:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)

w ^我需要改變爲像第一種方法返回?

附加信息: 這是我敢繼承一個倉庫的基類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Data; 
using System.Data.SqlClient; 
using Dapper; 


namespace AutorizarCreditoApp.Repositories 
{ 
    public abstract class BaseRepository 
    { 
     private readonly string _ConnectionString; 

     protected BaseRepository(string connectionString) 
     { 
      _ConnectionString = connectionString; 
     } 

     protected async Task<T> WithConnection<T>(Func<IDbConnection, Task<T>> getData) 
     { 
      try 
      { 
       using (var connection = new SqlConnection(_ConnectionString)) 
       { 
        await connection.OpenAsync(); // Asynchronously open a connection to the database 
        return await getData(connection); // Asynchronously execute getData, which has been passed in as a Func<IDBConnection, Task<T>> 
       } 
      } 
      catch (TimeoutException ex) 
      { 
       throw new Exception(String.Format("{0}.WithConnection() experienced a SQL timeout", GetType().FullName), ex); 
      } 
      catch (SqlException ex) 
      { 
       throw new Exception(String.Format("{0}.WithConnection() experienced a SQL exception (not a timeout)", GetType().FullName), ex); 
      } 
     } 

    } 
} 
+0

嗯......我改爲toListAsync()方法,因爲你建議,但是...不可用,但... VS建議我添加EntityFramework軟件包。不知道該怎麼做,因爲我使用的是Dapper,而不是EF。 –

+0

是的,沒有看到...評論刪除 – ghg565

回答

2

我看到您的代碼示例的兩個問題。

首先,只要使用.ToList()await後。換句話說,您不需要await行上的.ToList()

其次,請使用參數化查詢 - Dapper supports it。您通常使用匿名類型的對象來聲明參數值並將該對象作爲param參數傳遞。對於存儲過程,似乎您需要知道這些參數本身的名稱,而不是將它們作爲SQL的一部分提供。因此,下面我假定您的存儲過程需要三個名爲@Id@SomeNumber@CashOrCredit的參數。

有了這兩個建議,你的代碼可能是這樣的:

public async Task<List<ClientSummaryModel>> GetClientSummaryCredit(string id) 
{ 
    return await WithConnection(async c => 
    { 
     c.ChangeDatabase("PAQA"); 
     var summaryClientList = 
      await c.QueryAsync<ClientSummaryModel>("dbo.SP_GET_CLIENT_SUMMARY", 
                new 
                { 
                 Id = id, 
                 SomeNumber = 0, 
                 CashOrCredit = "CREDIT" 
                }, 
                commandType: CommandType.StoredProcedure); 
     return summaryClientList.ToList(); 
    }); 
} 

注:我認爲您是在這個文件using System.Linq;