When is it recommended that a class implement Idisposable?

Asked

Viewed 525 times

10

I use the IOC standard in my project which facilitates the call of procedure type Resolver.Resolve<IPedido>().GerarPedido(), however, I did not want to leave "loose" in this way, I would like to implement the method IDisposable wear like this:

using (var pedido = Resolver.Resolve<IPedido>())
{
  pedido.GerarPedido();
}
  1. I can implement IDisposable in a simple class, in this example, Pedido? It would be good practice?
  2. Has processing cost?
  3. How best to implement IDisposable in this example?
  • 2

    For a class to have to implement Idisposable there has to be a reason, in your case what is?

  • Well, in my mind, I wanted to matar Resolver.Resolve<IPedido>().GerarPedido() soon after its execution. The class Pedido is a basic class, does not use any external component.

  • @rubStackOverflow What "kill" the request means?

  • Remove from memory :) @dcastro

2 answers

7

  1. You can implement, of course. You can do whatever you want.

  2. There is processing cost. Whether it will be a problem or not, it is necessary to evaluate.

  3. The best way is not to implement. After all it doesn’t seem that this uses some external resource that the application has no control over, so it doesn’t make sense to implement it.

Unless you have something obscure within this request you have no reason to implement this interface and adopt the layout pattern. And if you have a reason, this class is probably implemented wrong. I cannot give details because the question does not give details about the class.

The object must be claimed by Garbage Collector at the right time and will not be released. If this is not happening it is because you have problems. GC is smart, he knows when to do it. It just isn’t miraculous, if something is preventing it from collecting has to solve this problem.

Read more on the subject.

About the memory management. NET.

  • Got it, like @ramaral deve-se ter uma razão to use Idisposable, in the case of the example, the GC does, since, Pedido is a simple class and uses no external components.

7


The function of the block using{}, using your words, it’s not "killing" an object so that it "doesn’t get loose".
This is the responsibility of Garbage Collector.

The method Dispose(), that is part of the interface Idisposable, is usually used to release Resources allocated by the class which, if not released, could create memory Leaks when the object was collected by the AG.

The function of using is to allow classes that implement Idisposable to be used in order to ensure the execution of the method Dispose() at the end of its use, even if an exception is made.

Thus, the implementation of the interface Idisposable is recommended only where it is necessary to carry out a procedure after use of the object and before it is collected by the GC.

Implementation of this interface alone does not guarantee that the method Dispose() is called, but indicates to the user/consumer that it should.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.