.NET核心中依赖类的依赖注入生命周期
我想知道,在请求后,处理所有iDisposable对象的最佳方法是什么。
addTransient< t>
- 添加每次创建的类型 要求。addScoped< t>
- 添加了一种用于请求范围的类型。添加Singleton< t>
- 首次请求并保留时添加类型 持有它。
因此,Singleton不能成为一个不错的选择,因为它会在应用程序被击落后处置。但是范围和瞬态是良好的候选人。我有一个存储库,我想与我的数据库建立一个连接:
public class Dapperr : IDapper
{
private readonly IConfiguration _config;
private string Connectionstring = "DefaultConnection";
public Dapperr(IConfiguration config)
{
_config = config;
}
public void Dispose()
{
}
public T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.Text)
{
using IDbConnection db = new SqlConnection(_config.GetConnectionString(Connectionstring));
return db.Query<T>(sp, parms, commandType: commandType).FirstOrDefault();
}
public List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)
{
using IDbConnection db = new SqlConnection(_config.GetConnectionString(Connectionstring));
return db.Query<T>(sp, parms, commandType: commandType).ToList();
}
}
现在在我的启动中,我将添加依赖项注入:
services.AddScoped<IDapper, Dapperr>();
我想知道我是否允许使用所有这些使用所有这些
scop是因为添加了范围依赖性。
这样:
public class Dapperr : IDapper
{
private readonly IConfiguration _config;
private string Connectionstring = "DefaultConnection";
private readonly IDbConnection db ;
public Dapperr(IConfiguration config)
{
_config = config;
db = new SqlConnection(_config.GetConnectionString(Connectionstring));
}
public T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.Text)
{
return db.Query<T>(sp, parms, commandType: commandType).FirstOrDefault();
}
public List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)
{
return db.Query<T>(sp, parms, commandType: commandType).ToList();
}
}
SQL连接在请求结束后是否处置或我仍然需要使用使用?
I want to know, what is the best way to dispose the all IDisposable object after the request done.
AddTransient<T>
- adds a type that is created again each time it's
requested.AddScoped<T>
- adds a type that is kept for the scope of the request.AddSingleton<T>
- adds a type when it's first requested and keeps
hold of it.
So, singleton could not be a good choice because it will disposes after app shot down. but scope and transient are good candidates. I have a repository which I want to create a connection with my db like this:
public class Dapperr : IDapper
{
private readonly IConfiguration _config;
private string Connectionstring = "DefaultConnection";
public Dapperr(IConfiguration config)
{
_config = config;
}
public void Dispose()
{
}
public T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.Text)
{
using IDbConnection db = new SqlConnection(_config.GetConnectionString(Connectionstring));
return db.Query<T>(sp, parms, commandType: commandType).FirstOrDefault();
}
public List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)
{
using IDbConnection db = new SqlConnection(_config.GetConnectionString(Connectionstring));
return db.Query<T>(sp, parms, commandType: commandType).ToList();
}
}
Now In my start up I'm going to add dependency injection:
services.AddScoped<IDapper, Dapperr>();
I want to know if I am allow to remove All those using
scop because of adding scope dependency. for example like this:
public class Dapperr : IDapper
{
private readonly IConfiguration _config;
private string Connectionstring = "DefaultConnection";
private readonly IDbConnection db ;
public Dapperr(IConfiguration config)
{
_config = config;
db = new SqlConnection(_config.GetConnectionString(Connectionstring));
}
public T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.Text)
{
return db.Query<T>(sp, parms, commandType: commandType).FirstOrDefault();
}
public List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)
{
return db.Query<T>(sp, parms, commandType: commandType).ToList();
}
}
does the sql connection dispose after request ended or I still need to use using
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在阅读了评论后,我必须将接口设置为
iDisposable
以处理连接,因此我更改了代码:然后在我的回购中,我实现了Dispose方法:
调试后,我看到此
处置
方法被调用并处理连接。我不确定这是最佳实践,但是通过这些更改,我只使用 bloc删除了一次连接配置。我认为这适合轻请求。After reading the comments I got that, I have to set the the interface as
IDisposable
to dispose the connection, so I changed my code like this:then in my repo, I implemented dispose method:
After debugging, I saw this
Dispose
method is called and connection disposed. I'm not sure this is the best practice but by these changes I only wrote the connection config once and all myusing
blocs deleted. I think this would be good for light requests.