U
U
User992021-04-08 13:11:59
ASP.NET
User99, 2021-04-08 13:11:59

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

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

}

i.e. is this line enough:

public void Dispose()
{
Dispose();
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-04-08
@vabka

A recursion has occurred.
Probably you wanted to disable Connection

R
Roman, 2021-04-09
@yarosroman

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 question

Ask a Question

731 491 924 answers to any question