When exposing an IQueryable when deleting a DataContext?

It seems popular at the moment if you implement the repository as simple

IQueryable<T> FetchAll<T>();

using LINQ to SQL, then the repository should set the DataContext, which remains available outside the repository.

So my question is: how does a DataContext get Disposed? What if an exception is thrown by code outside the repository? Will it be a database connection leak?

thanks

+3
source share
2 answers

Why you do not need to call dispose in the DataContext

Summary DataContext opens a connection when a request is invoked (when accessing data) and closes it when the request is completed.

+3
source

IDisposable ( DataContext, Disposed). API - -

using (var repository=new MyRepository) //or use a ServiceLocator or Factory
{
    var myObjects = repository.FetchAll().Where(obj=>obj.Foo == "bar");
    //do something with myObjects
}

DataContext, .

+2

Source: https://habr.com/ru/post/1716842/


All Articles