.NET Core 中 IDisposable 类的依赖注入生命周期

Ali*_*ghi 6 c# dependency-injection dapper .net-core asp.net-core

我想知道,请求完成后处理所有 IDisposable 对象的最佳方法是什么。

  • AddTransient<T>- 添加每次请求时再次创建的类型。
  • AddScoped<T>- 添加为请求范围保留的类型。
  • AddSingleton<T>- 在第一次请求时添加类型并保留它。

因此,单例可能不是一个好的选择,因为它会在应用程序被击落后进行处理。但范围和瞬态是不错的选择。我有一个存储库,我想与我的数据库创建连接,如下所示:

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();  
        }  
}
Run Code Online (Sandbox Code Playgroud)

现在在我的启动中我将添加依赖项注入:

services.AddScoped<IDapper, Dapperr>();
Run Code Online (Sandbox Code Playgroud)

我想知道是否允许删除所有这些using范围,因为添加了范围依赖项。例如这样:

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();  
        }
  
    }
Run Code Online (Sandbox Code Playgroud)

sql 连接是否在请求结束后释放,还是我仍然需要使用using

Ali*_*ghi 1

阅读评论后,我得到了这一点,我必须将接口设置为IDisposable处置连接,所以我更改了代码,如下所示:

public interface IDapper : IDisposeable
{
    ... 
}
Run Code Online (Sandbox Code Playgroud)

然后在我的仓库中,我实现了 dispose 方法:

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) =>
        db.Query<T>(sp, parms, commandType: commandType).ToList();  
    
    public void Dispose()
    {
        db?.dispose(); 
    }
}
Run Code Online (Sandbox Code Playgroud)

调试后,我看到这个Dispose方法被调用并且连接被释放。我不确定这是最佳实践,但通过这些更改,我只编写了一次连接配置,并且using删除了所有块。我认为这对于轻微的请求来说是有好处的。