4

I am using Factory pattern to create .NET objects of a class. I also need to make sure that all such objects should be disposed before application terminates.

Where and How can I dispose the objects created by factory pattern? Shall I dispose in the class in which I am getting the objects created by factory?

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
Ram
  • 11,404
  • 15
  • 62
  • 93

2 Answers2

8

When your factory creates new IDisposable objects, the caller should normally dispose such an object. An advisable pattern is the following:

using (var instance = Factory.CreateInstance(someArg))
{
    // use the instance
}

When your Factory uses some internal pool, then it is still advisable to let the caller dispose the object, but in that case, as soon as the instance is disposed, it should be returned to the pool. However, such a design is much more complicated.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • 1
    But the factory method returns an object just by a custom interface, not IDisposable, how then would it work with the using statement? "Type used in a using statement must be implicitly convertible to 'System.IDisposable'". – SerG Jan 14 '14 at 09:39
  • 1
    @SerG in that case, the factory must register the returned instance for disposal, and there must be a mechanism in place that allows disposing such object when a certain scope (such as a web request) ends. IoC frameworks can help with this. – Steven Jan 14 '14 at 10:38
  • Ok, I'll take it into consideration, but now I just added to my interface the Close method. – SerG Jan 14 '14 at 10:45
  • 2
    @Serg: If you add a `Close` method to your interface, you can just as well let that interface inherit from `IDisposable`. I would say that is even a wiser thing to do, since the contract of `IDisposable` is very clear and it makes deterministic disposal much easier for the consumers (since they can use the `using` statement). – Steven Jan 14 '14 at 11:47
  • Oh, really. I just forgot that interfaces can inherit each other. – SerG Jan 14 '14 at 12:22
  • Maybe more right way is checking object for implementation of IDisposable: `var d = instance as IDisposable; if (d != null) d.Dispose();` – SerG Jan 28 '14 at 07:23
  • 1
    @SerG: That would certainly NOT be a more right way to do it. That just complicates your code an will ensure that other developers will forget to dispose those objects since the contract doesn't describe clearly that objects need to be disposed. – Steven Jan 28 '14 at 07:31
  • But what if somebody implements my interface without using of object must be disposed? So he need write an empty stub method Dispose()? All these difficulties make me want create finalizer. – SerG Jan 28 '14 at 09:41
  • It smells like a crutch. – SerG Jan 28 '14 at 12:46
5

Why do you want to dispose them before the application terminates? Is this because they hold unmanaged resources?

If this is the case, just implement IDisposable and perform the cleanup in the Dispose method, and let .Net take care of the rest.

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
  • They do not hold any unmanaged resources. I need to dispose those objects to clear the memory. – Ram Apr 19 '10 at 09:09
  • 4
    .Net does this for you as soon as the application ends. – Gerrie Schenck Apr 19 '10 at 09:15
  • .Net will dispose of these objects once the objects are no longer being used (there is nothing referencing them). This can be and often is way before the application ends! – Mongus Pong Apr 19 '10 at 10:17
  • 3
    .NET will never call Dispose for you, so implementing dispose is useless if you want the framework to clean up when the application ends. – Steven Jan 14 '14 at 10:41
  • I have to dispose them because of FxCop and Roslyn rules =) – isxaker Feb 09 '17 at 12:59
  • 2
    this answer is incomplete. If you just implement IDisposable.Dispose() nobody will call it (see [this question](https://stackoverflow.com/questions/6967108/is-idisposable-dispose-called-automatically)) unless you're using the keyword `using` or YOUR code is calling .Dispose on the object. You should say that the finalizer should be implemented so the garbage collector will clear up the object. – peval27 May 17 '19 at 11:07