Solved

Considerations when having IDisposable private member variables

Posted on 2016-09-24
4
56 Views
Last Modified: 2016-09-25
I have a class similar to below which has a private member variable whose type is from a 3rd party and implements IDisposable.  My question is are there any considerations I should be making when using a type this way?

    public class PaypalService
    {
        //PayPalAPIInterfaceClient implements IDispose
        private readonly PayPalAPIInterfaceClient _paypalClient = new PayPalAPIInterfaceClient();

        public List<PaypalTransaction> GetTransactions(DateTime startDate, DateTime endDate)
        {
            //do stuff using _paypalClient
        }
        public List<PaypalTransaction> GetTransactions(string payerEmail)
        {
            //do stuff using _paypalClient
        }
        public List<PaypalTransaction> GetTransaction(string transactionId)
        {
            //do stuff using _paypalClient
        }
    }

Open in new window



In all the code examples the class is wrapped in a using() similar to below which is what made me think I might be causing problems using it as a member variable.

            using (var paypalClient = new PayPalAPIInterfaceClient())
            {
                //do stuff using paypalClient
            }

Open in new window

0
Comment
Question by:canuckconsulting
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41814051
If the disposable thing is at the class level, then your class needs to implement IDisposable so that other developers have the chance to dispose of the object. You will dispose of the class-level object in the Dispose method you create. Otherwise, if you're only using something disposable within a method, then wrap that usage in a using statement.
0
 

Author Comment

by:canuckconsulting
ID: 41814052
Hi käµfm³d.

It's used by multiple members within the class which is why I have it set as a member variable.

What would the IDisposable method look like?  Would it simply call _paypalClient .Dispose()?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 41814063
Yes, but you should follow the disposable pattern when you implement it, as recommended by MS.

By creating a public Dispose method, you tell other developers that they should expect to clean up this class (via that method) when they are done with it.
0
 

Author Closing Comment

by:canuckconsulting
ID: 41814936
Great link.  Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now