Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

Class both stores and returns disposable object

This code I came across must violate some basic principle of good programming practices. I'm wondering what that principle is, and how can this be refactored to make it healthier.

Here's the basic outline:
public class XBase
{
    private DisposableThing d;
    protected DisposableThing D
    {
        get { return d; }
        set {
              d?.Dispose();
              d=value;
            }
    }

    public abstract GetDisposableThing(...);

    public DisposableThing CreateDisposableThing()
    {
        return new DisposableThing(...);
    }
}

public class X : XBase, IDisposable
{
    public override DisposableThing GetDisposableThing(...)
    {
        this.D = CreateDisposableThing();
        return this.D;
    }
}

public class Main
{
    Public void Test()
    {
        DisposableThing a = GetDisposableThing(...);
        DisposableThing b = GetDisposableThing(...);
        // now a has been disposed of, perhaps unexpectedly.
    }
}

Open in new window

Hopefully the comment at the end makes the problem clear.

I'm not sure what the original designers of class XBase had in mind, or how the code ended up being this way.

The weird thing is class XBase provides a place to store DisposableThing as property D with backing field d; however, nowhere in class XBase is D or d actually set to anything.

The derived class X defines GetDisposableThing() as "call my CreateDisposableThing() and store the result in my D property, and return the result to the caller."

The problem seems to be class X is both storing something and returning that something to the outside world, leaving us with that something being in two places.

I'm not sure how to untangle this. (Is it even possible?)
Avatar of ste5an
ste5an
Flag of Germany image

I have a problem with the fact, that someone seemed to reinvent the wheel. The .Net way is to use IDisposable, not to introduce a DisposableThing.
Also the kind of factory method approach is more than weird. Cause you call a non-static method in your main.. are there some modifiers in your outline missing?
Avatar of deleyd

ASKER

I apologize the Main class isn't actually the Main class. I should have called it something else. I wanted to show that the code as it is does something unexpected, where it disposes of something it's handed out.

The sample Main code isn't how it's currently used. Currently it's used something like:

x = new X(...);
loop
{
    DisposableThing a = x.GetDisposableThing(...);
    use a for something
}

Open in new window

It works as long as the user follows this pattern. However,... I fear this code will be difficult to maintain as is. it will confuse others as it confused me. (I'm still trying to make sense of it. Why was it written this way?)

DisposableThing is something like an Image or BItmap or something, I forget what it is (I don't have the code in front of me right now).

Could this be refactored to make more sense?
Avatar of samuel
samuel

Class 'XBase' must be marked with the abstract modifier, in order to have the abstract member 'GetDisposableThing'. This program will not compile.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial