2016-02-01 26 views
1

我正在使用ORM連接到它稱爲dapper的數據庫。問題在於它的數據庫調用是同步的,我最近通過遵循這個簡短的教程http://www.joesauve.com/async-dapper-and-async-sql-connection-management/找到了使其異步的方法。我的問題是如何將此BaseRepository帶入我的Controller類?這是該網站上的代碼和它的同一個我的方式MVC 6如何在我的控制器類中包含BaseRepository

BaseRepository-沒有問題,在此代碼

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); 
    } 
} 

}

,現在他帶來了它在這樣

public class PersonRepository : BaseRepository 
{ 
    public PersonRepository(string connectionString): base (connectionString) { } 

    public async Task<Person> GetPersonById(Guid Id) 
    { 
     return await WithConnection(async c => { 

      // Here's all the same data access code, 
      // albeit now it's async, and nicely wrapped 
      // in this handy WithConnection() call. 
      var p = new DynamicParameters(); 
      p.Add("Id", Id, DbType.Guid); 
      var people = await c.QueryAsync<Person>(
       sql: "sp_Person_GetById", 
       param: p, 
       commandType: CommandType.StoredProcedure); 
      return people.FirstOrDefault(); 

     }); 
    } 
} 

我有一個問題的部分是公共類PersonRepository:BaseRepository因爲Asp.Net控制器以公共類HomeController開始:控制器,我需要訪問WithConnection方法才能使其工作。我的控制器看起來像這樣

public class HomeController : Controller 
    { 

     public class ConnectionRepository : BaseRepository 
     { 
      public ConnectionRepository(string connectionString) : base(connectionString) { } 

     } 
     public async Task<ActionResult> topfive() 
      { 
    // I get Error on WithConnection as it can't see the BaseRepository 
       return await WithConnection(async c => 
       { 


        var topfive = await c.QueryAsync<Streams>("select * from streams ").ToList(); 
       return View(topfive); 
       }); 

} 
      } 

我顯然不能掩蓋我與BaseRepository ActionResult的方法,因爲它提供了所有類型的錯誤有什麼建議嗎?

回答

2

爲什麼使用繼承而不是組合?關於像什麼:在MVC 6

public class PersonRepository : BaseRepository 
{ 
    public PersonRepository(string connectionString): base (connectionString) { } 

    public async Task<Person> GetPersonById(Guid Id) 
    { 
     return await WithConnection(async c => { 

      // Here's all the same data access code, 
      // albeit now it's async, and nicely wrapped 
      // in this handy WithConnection() call. 
      var p = new DynamicParameters(); 
      p.Add("Id", Id, DbType.Guid); 
      var people = await c.QueryAsync<Person>(
       sql: "sp_Person_GetById", 
       param: p, 
       commandType: CommandType.StoredProcedure); 
      return people.FirstOrDefault(); 

     }); 
    } 
    } 

     public class ConnectionRepository : BaseRepository 
     { 
      public ConnectionRepository(string connectionString) : base(connectionString) { } 

     } 
     public async Task<List<TopFileClass>> topfive() 
      { 
    // I get Error on WithConnection as it can't see the BaseRepository 
       return await WithConnection(async c => 
       { 


        var topfive = await c.QueryAsync<Streams>("select * from streams ").ToList(); 
       return topfive; 
       }); 

} 

public class HomeController : Controller 
{ 
    private readonly PersonRepository _repo; 

    public HomeController(PersonRepository repo) 
    { 
     _repo = repo; 
    } 

    public async Task<ActionResult> TopFive() 
    { 
     var top5 = await _repo.topfive(); 
     return View(top5); 
    } 
} 

如果你不熟悉如何使資源庫自動獲得注入的構造函數,閱讀了依賴注入

1

您必須從「控制器」構建「BaseRepository」。我認爲這會對你有用。那麼只需要使用下面的代碼:

public abstract class BaseRepository : Controller 
{ 
// do you work 
} 


public class PersonRepository : BaseRepository 
{ 
public PersonRepository(string connectionString): base (connectionString) { } 

public async Task<Person> GetPersonById(Guid Id) 
{ 
    return await WithConnection(async c => { 

     // Here's all the same data access code, 
     // albeit now it's async, and nicely wrapped 
     // in this handy WithConnection() call. 
     var p = new DynamicParameters(); 
     p.Add("Id", Id, DbType.Guid); 
     var people = await c.QueryAsync<Person>(
      sql: "sp_Person_GetById", 
      param: p, 
      commandType: CommandType.StoredProcedure); 
     return people.FirstOrDefault(); 

    }); 
    } 
} 
相關問題