• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 61
  • Last Modified:

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?)
0
deleyd
Asked:
deleyd
  • 2
1 Solution
 
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
 
deleydAuthor 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now