.NET核心中依赖类的依赖注入生命周期

发布于 2025-01-17 09:38:41 字数 2579 浏览 2 评论 0原文

我想知道,在请求后,处理所有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩于岁月 2025-01-24 09:38:41

在阅读了评论后,我必须将接口设置为iDisposable以处理连接,因此我更改了代码:

public interface IDapper : IDisposeable
{
    ... 
}

然后在我的回购中,我实现了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(); 
    }
}

调试后,我看到此处置方法被调用并处理连接。我不确定这是最佳实践,但是通过这些更改,我只使用 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文