Ali Eshghi
Ali Eshghi

Reputation: 1233

dependency injection life cycle for IDisposable classes in .NET core

I want to know, what is the best way to dispose the all IDisposable object after the request done.

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?

Upvotes: 6

Views: 1725

Answers (1)

Ali Eshghi
Ali Eshghi

Reputation: 1233

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:

public interface IDapper : IDisposeable
{
    ... 
}

then in my repo, I implemented dispose method:

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

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 my using blocs deleted. I think this would be good for light requests.

Upvotes: 1

Related Questions