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?)
deleydSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
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?
1
deleydSoftware EngineerAuthor Commented:
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?
1
samuel.NET developerCommented:
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
0
ste5anSenior DeveloperCommented:
As long as your outline hasn't missed something important:

I would get rid of it. It seems like someone has misunderstood IDisposable and owenership. And also factory methods. XBase is a wrapper for imho incorrect owenership handling. And  is a "generic" derivation, without any additional benefit.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.