locked
IDisposable Interface RRS feed

  • Question

  • User-203654341 posted

    Hi,

    I am using entity framework in my application and i know one of making sure the context is disposed is by using @using(). I don't want to use above method but instead inherit the class from IDisposable interface &  implement the dispose method. How does .net know when to call the Dispose method??

    Tuesday, September 11, 2018 5:56 PM

Answers

  • User1120430333 posted

    Since opening & closing a connection is expensive process, instead of closing everytime after entity is populated i thought we can inherit IDisposable interface & Override dispose method.

    No it's not expensive. And if you don't dispose of the context where a Linq query created a collection of objects, you never disposed of the context , and you looped over the connected collection, then you have read the same data twice. 100,000 objects in the collection  is 100,000 reads due to the query, and you read the objects from the database 100,000 more times on the loop as each object is read again from the database due to not disconnecting the collection from the database to disconnect the collection.   

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 15, 2018 6:30 AM
  • User-821857111 posted

    Since opening & closing a connection is expensive process, instead of closing everytime after entity is populated i thought we can inherit IDisposable interface & Override dispose method. 
    Your assumptions are not necessarily correct. 

    First, it is not necessary to call Dispose on a DbContext. The default behavior of DbContext is that the underlying connection is automatically opened any time it is needed and closed when it is no longer needed: https://blog.jongallant.com/2012/10/do-i-have-to-call-dispose-on-dbcontext/

    Second, although the context is disposed by the framework, underlying connections are subject to connection pooling. The .NET connection is closed in code, but the actual connection is usually returned to the pool where it is available for reuse.

    Personally, I wouldn't ever waste my time overriding the default behaviour of components within the .NET framework unless I knew for certain that there was an issue that needs to be resolved. And before I did that, I would log a bug with the developer team on GitHub.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 17, 2018 6:46 AM

All replies

  • User-821857111 posted

    The DbContext and any classes that derive from it already implements IDisposable. What exactly are you trying to do?

    .NET doesn't call Dispose. That is something that the developer does. The recommended way is to use a using block.

    Tuesday, September 11, 2018 8:56 PM
  • User1520731567 posted

    Hi Chethan1221,

    From this article:

    When you use an object that accesses unmanaged resources, a good practice is to create the instance with a using statement. The using statement automatically closes the stream and calls Dispose on the object when the code that is using it has completed.

    More details,you could refer to:

    https://stackoverflow.com/questions/8440230/when-does-the-dispose-method-get-called

    Best Regards.

    Yuki Tao

    Wednesday, September 12, 2018 6:32 AM
  • User1120430333 posted

    Unless you are using an IoC that is controlling the lifetime of Dbcontext, then you not using a "using" statement do dispose of Dbconext is not optimal programming.

    Wednesday, September 12, 2018 11:15 AM
  • User-203654341 posted

    Sorry for not getting back to you guys. Here is what i am trying to do

    Since opening & closing a connection is expensive process, instead of closing everytime after entity is populated i thought we can inherit IDisposable interface & Override dispose method. 

           protected virtual void Dispose(bool disposing)
            {
                if (_disposed)
                    return;
                if (Context != null)
                    Context.Dispose();
                Context = null;
                _disposed = true;
            }
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }

    Now i am thinking where to call the above dispose method?? May be from Application_Stop in Global.asax 

    Friday, September 14, 2018 10:14 PM
  • User1120430333 posted

    Since opening & closing a connection is expensive process, instead of closing everytime after entity is populated i thought we can inherit IDisposable interface & Override dispose method.

    No it's not expensive. And if you don't dispose of the context where a Linq query created a collection of objects, you never disposed of the context , and you looped over the connected collection, then you have read the same data twice. 100,000 objects in the collection  is 100,000 reads due to the query, and you read the objects from the database 100,000 more times on the loop as each object is read again from the database due to not disconnecting the collection from the database to disconnect the collection.   

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 15, 2018 6:30 AM
  • User-821857111 posted

    Since opening & closing a connection is expensive process, instead of closing everytime after entity is populated i thought we can inherit IDisposable interface & Override dispose method. 
    Your assumptions are not necessarily correct. 

    First, it is not necessary to call Dispose on a DbContext. The default behavior of DbContext is that the underlying connection is automatically opened any time it is needed and closed when it is no longer needed: https://blog.jongallant.com/2012/10/do-i-have-to-call-dispose-on-dbcontext/

    Second, although the context is disposed by the framework, underlying connections are subject to connection pooling. The .NET connection is closed in code, but the actual connection is usually returned to the pool where it is available for reuse.

    Personally, I wouldn't ever waste my time overriding the default behaviour of components within the .NET framework unless I knew for certain that there was an issue that needs to be resolved. And before I did that, I would log a bug with the developer team on GitHub.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 17, 2018 6:46 AM