2016-08-02 48 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;