Asp.net mvc MVC 6如何在控制器类中包含BaseRepository
我正在使用一个ORM连接到数据库,它被称为dapper。它的问题是它的数据库调用是同步的,我最近发现了一种方法,通过遵循这个简短的教程使它异步。我的问题是如何将此BaseRepository引入控制器类?这是那个网站上的代码,和我的代码一样 BaseRepository——顺便说一下,这段代码中没有任何问题Asp.net mvc MVC 6如何在控制器类中包含BaseRepository,asp.net-mvc,asp.net-core,asp.net-core-mvc,dapper,Asp.net Mvc,Asp.net Core,Asp.net Core Mvc,Dapper,我正在使用一个ORM连接到数据库,它被称为dapper。它的问题是它的数据库调用是同步的,我最近发现了一种方法,通过遵循这个简短的教程使它异步。我的问题是如何将此BaseRepository引入控制器类?这是那个网站上的代码,和我的代码一样 BaseRepository——顺便说一下,这段代码中没有任何问题 public abstract class BaseRepository { private readonly string _ConnectionString; protec
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);
}
}
公共抽象类基本存储库
{
私有只读字符串_ConnectionString;
受保护的BaseRepository(字符串连接字符串)
{
_ConnectionString=ConnectionString;
}
带连接的受保护异步任务(Func getData)
{
试一试{
使用(var connection=newsqlconnection(_ConnectionString)){
等待连接。OpenAsync();//异步打开与数据库的连接
return wait getData(connection);//异步执行getData,它已作为Func传入
}
}
捕获(TimeoutException例外){
抛出新异常(String.Format(“{0}.WithConnection()遇到SQL超时”,GetType().FullName),ex);
}
catch(SqlException-ex){
抛出新异常(String.Format(“{0}.WithConnection()遇到SQL异常(不是超时)”,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();
});
}
}
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);
});
}
}
公共类PersonRepository:BaseRepository
{
公共PersonRepository(string connectionString):基本(connectionString){}
公共异步任务GetPersonById(Guid Id)
{
返回wait with connection(异步c=>{
//这是所有相同的数据访问代码,
//尽管现在它是异步的,并且包装得很好
//在这个方便的WithConnection()调用中。
var p=新的动态参数();
p、 添加(“Id”、Id、DbType.Guid);
var people=wait c.QueryAsync(
sql:“sp_Person_GetById”,
参数:p,
commandType:commandType.StoredProcess);
返回人。FirstOrDefault();
});
}
}
我遇到问题的部分是这个公共类PersonRepository:BaseRepository,因为Asp.Net控制器以公共类HomeController:Controller开头,我需要访问with connection方法才能工作。我的控制器看起来像这样
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 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);
});
}
}
公共类HomeController:控制器
{
公共类ConnectionRepository:BaseRepository
{
公共ConnectionRepository(string connectionString):基本(connectionString){}
}
公共异步任务topfive()
{
//我在WithConnection上出错,因为它无法看到BaseRepository
返回wait with connection(异步c=>
{
var topfive=wait c.QueryAsync(“从流中选择*).ToList();
返回视图(前五名);
});
}
}
显然,我不能用BaseRepository覆盖我的ActionResult方法,因为它会给所有类型的错误提供任何建议?您必须从“控制器”中插入“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();
});
}
}
公共抽象类基本存储库:控制器
{
//你工作吗
}
公共类PersonRepository:BaseRepository
{
公共PersonRepository(string connectionString):基本(connectionString){}
公共异步任务GetPersonById(Guid Id)
{
返回wait with connection(异步c=>{
//这是所有相同的数据访问代码,
//尽管现在它是异步的,并且包装得很好
//在这个方便的WithConnection()调用中。
var p=新的动态参数();
p、 添加(“Id”、Id、DbType.Guid);
var people=wait c.QueryAsync(
sql:“sp_Person_GetById”,
参数:p,
commandType:commandType.StoredProcess);
返回人。FirstOrDefault();
});
}
}
为什么要使用继承而不是组合?比如说:
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);
}
}
公共类PersonRepository:BaseRepository
{
公共PersonRepository(string connectionString):基本(connectionString){}
公共异步任务GetPersonById(Guid Id)
{
返回wait with connection(异步c=>{
//这是所有相同的数据访问代码,
//尽管现在它是异步的,并且包装得很好
//在这个方便的WithConnection()调用中。
var p=新的动态参数();
p、 添加(“Id”、Id、DbType.Guid);
var people=wait c.QueryAsync(
sql:“sp_Person_GetById”,
参数:p,
commandType:commandType.StoredProcess);
返回人。FirstOrDefault();
});
}
}
公共类ConnectionRepository:BaseRepository
{
公共ConnectionRepository(string connectionString):基本(connectionString){}
}
公共异步任务topfive()
{
//我在WithConnection上出错,因为它无法看到BaseRepository
返回wait with connection(异步c=>
{
var topfive=wait c.QueryAsync(“从流中选择*).ToList();
返回前五名;
});
}
公共类HomeController:控制器
{
私有只读PersonRepository_repo;
公共家庭控制器(PersonRepository repo)
{
_回购=回购;
}
公共异步任务TopFive()
{
var top5=等待_repo.top5();
返回视图(top5);
}
}
如果您不熟悉如何将存储库自动注入构造函数,请阅读MVC6中的依赖项注入