如何将我的数据库调用从 Web API 移动到 Class 库?
How to move my database calls from the Web API to Class Library?
我有 Web Api 有这个电话:
[HttpGet("getLocation")]
public async Task<IActionResult> GetLocation([FromQuery] int id)
{
var ConnectionString = new ConnectionStringsOptions();
_Configuration.GetSection(ConnectionStringsOptions.ConnectionStrings).Bind(ConnectionString);
using (IDbConnection cnn = new SqlConnection(ConnectionString.ConnectionString))
{
var _id = new
{
ID = id
};
string sql = @"SELECT * FROM [dbo].[fLocationFilter](@ID)";
var _Location = await cnn.QueryAsync<Location>(sql, _id);
return Ok(_Location);
}
}
我想将该调用从 API 移动到我的 class 库,如下所示:
public async Task<IActionResult> GetLocation([FromQuery] int id)
{
var _location= Mylibrery.getCall(string connectionString ,int id);
return Ok(_location);
}
我可以在不使用任务的情况下拨打电话吗?这会影响性能吗
我正在使用 class 库 .NET Core 5 和 ASP.NET Core 5。
更新:主要问题是如何获取从 class 库传递到 API
的 actionResult
您库中的代码可能如下所示:
public class Repository : IRepository
{
private readonly string _connectionString;
public Repository(IConfiguration configuration /* ILogger, etc */)
{
_connectionString = /* get connection string from configuration */
}
public async Task<Location> GetLocation(int id)
{
using (IDbConnection cnn = new SqlConnection(_connectionString))
{
var _id = new { ID = id };
string sql = @"SELECT * FROM [dbo].[fLocationFilter](@ID)";
var location = await cnn.QueryAsync<Location>(sql, _id);
return location;
}
}
}
它在控制器中的使用看起来像这样:
public class SomeController
{
private readonly IRepository _repository;
public SomeController(IRepository repository /* ILogger, etc */)
{
_repository = repository;
}
[HttpGet("getLocation")]
public async Task<IActionResult> GetLocation([FromQuery] int id)
{
var location = await _repository.GetLocation(id);
return Ok(location);
}
}
如果你想获得服务的整体性能,你绝对应该使用任务,或者更确切地说,异步代码。
我有 Web Api 有这个电话:
[HttpGet("getLocation")]
public async Task<IActionResult> GetLocation([FromQuery] int id)
{
var ConnectionString = new ConnectionStringsOptions();
_Configuration.GetSection(ConnectionStringsOptions.ConnectionStrings).Bind(ConnectionString);
using (IDbConnection cnn = new SqlConnection(ConnectionString.ConnectionString))
{
var _id = new
{
ID = id
};
string sql = @"SELECT * FROM [dbo].[fLocationFilter](@ID)";
var _Location = await cnn.QueryAsync<Location>(sql, _id);
return Ok(_Location);
}
}
我想将该调用从 API 移动到我的 class 库,如下所示:
public async Task<IActionResult> GetLocation([FromQuery] int id)
{
var _location= Mylibrery.getCall(string connectionString ,int id);
return Ok(_location);
}
我可以在不使用任务的情况下拨打电话吗?这会影响性能吗
我正在使用 class 库 .NET Core 5 和 ASP.NET Core 5。
更新:主要问题是如何获取从 class 库传递到 API
您库中的代码可能如下所示:
public class Repository : IRepository
{
private readonly string _connectionString;
public Repository(IConfiguration configuration /* ILogger, etc */)
{
_connectionString = /* get connection string from configuration */
}
public async Task<Location> GetLocation(int id)
{
using (IDbConnection cnn = new SqlConnection(_connectionString))
{
var _id = new { ID = id };
string sql = @"SELECT * FROM [dbo].[fLocationFilter](@ID)";
var location = await cnn.QueryAsync<Location>(sql, _id);
return location;
}
}
}
它在控制器中的使用看起来像这样:
public class SomeController
{
private readonly IRepository _repository;
public SomeController(IRepository repository /* ILogger, etc */)
{
_repository = repository;
}
[HttpGet("getLocation")]
public async Task<IActionResult> GetLocation([FromQuery] int id)
{
var location = await _repository.GetLocation(id);
return Ok(location);
}
}
如果你想获得服务的整体性能,你绝对应该使用任务,或者更确切地说,异步代码。