Answer the question
In order to leave comments, you need to log in
Am I using IDisposible C# ASP.NET Core correctly?
Am I using IDisposible in the repositories correctly so that when the using body is executed, it is destroyed?
Repository interface:
public interface IExamsRepository<T> where T:BaseEntity
{
Task<IList<Exam>> ShowAll();
Task New(Exam exam);
}
public class ReposExams : IExamsRepository<Exam>,IDisposible
{
private string connectionString;
public ReposExams(IConfiguration configuration)
{
connectionString = configuration.GetValue<string>("DBInfo:ConnectionString");
}
internal IDbConnection Connection
{
get
{
return new NpgsqlConnection(connectionString);
}
}
public void Dispose()
{
Dispose();
}
}
public void Dispose()
{
Dispose();
}
Answer the question
In order to leave comments, you need to log in
A recursion has occurred.
Probably you wanted to disable Connection
Why is IDispose here? You don't have unmanaged resources. NpgsqlConnection is a managed wrapper over the connection, everything will be collected and freed during garbage collection. Of course, if you have a highly loaded system, then it makes sense to release the connection. https://habr.com/ru/post/89720/ here, read how to do it right.
PS and create a connection in the repository, no ice, why did people come up with DI.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question