2

I have used the singleton pattern to create a single instance of a web service that I use in my win forms application. I have seen that this is good practice for web applications. I was wondering if this is the same for winform apps too? Also, should I worry about disposing of the web service (i.e. the proxy object afterwards) - it has a .Dispose method but I am not calling it anywhere in my code. In my application I am calling all my web methods asynchronously. This might sounds silly but I don't know where I need to call dispose. Can anyone help?

class ListService
{
    private static RetrieveList s_proxy;

    private static readonly object s_lock = new object();

    private static readonly string s_webServiceURL = Authentication.RetrieveListUrl;

    internal static RetrieveList Proxy
    {
        get
        {
            lock (s_lock)
            {
                if (s_proxy == null)
                {
                    s_proxy = new RetrieveList();
                    s_proxy.Url = s_webServiceURL;
                }

                return s_proxy;
            }
        }
    }

}
Christian Strempfer
  • 7,291
  • 6
  • 50
  • 75
bobbo
  • 845
  • 2
  • 14
  • 24
  • 5
    *Where* have you seen it claimed that using singletons is a good idea in a WinForms app? – Jon Skeet Mar 30 '12 at 15:26
  • 2
    Dealing with proxies is a bit more complicated than the code you have presented... If you're going to keep a proxy object in memory, you will have to add code to recreate it if the channel is faulted, ensure that it hasn't been closed, etc. – RQDQ Mar 30 '12 at 15:28
  • In the following post it mentions that this is a good idea. However, it appears to be in the context of a web and not winforms. http://stackoverflow.com/questions/429478/do-i-need-to-dispose-a-web-service-reference-in-asp-net – bobbo Mar 30 '12 at 15:35
  • @JonSkeet: isn't he just asking about that? He says _"I was wondering if this is the same for winform apps too"_. He doesn't know whether the claim for web apps would apply to WinForms also. – Abel Mar 30 '12 at 15:40
  • @Abel: Hmm... either I misread it before, or he's edited it then. Consider my question edited to "Where have you seen it claimed that using singletons is a good practice in web applications?" (Although the OP has now answered that question.) – Jon Skeet Mar 30 '12 at 15:41

2 Answers2

4

If you have a single instance for an application, the object will be destroyed when the application closes or crashes. You could call Dispose in the finalizer, but that's not guaranteed to succeed.

Unless Dispose does something vital like saving your document (it shouldn't), I'd say, don't worry too much. The fact it is a singleton means it lives forever and disposing is only meant to free resources that would otherwise remain around. The object remains around, so nothing to free.

However, some people may frown on this and say "what if the Dispose does something that is important to me, other than freeing resources?". You can still call Dispose after your application has run:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new LogAnalizzer());
    ListService.Dispose();   // or whatever you're going to call it
}

I have seen that this is good practice for web applications. I was wondering if this is the same for winform apps too?

As a rule of thumb, when your application as a whole needs only one instance of something, a (thread safe) singleton is often a good idea: a cache, connection to a database, a proxy, the application itself, a logger. There's nothing that says that singletons shouldn't apply to your WinForm apps also.

Remember however that you must carefully think about your design. What if you have a logger and the file becomes inaccessible? What if a proxy looses connection? etc, etc.

Abel
  • 56,041
  • 24
  • 146
  • 247
  • Thanks Abel. I appreciate your comments and feedback to my questions. I feel more confident with my direction now. – bobbo Mar 30 '12 at 15:48
3

Not sure how C# does it, But in Java, u need to create a private constructor to prevent people from doing new ListService()

Churk
  • 4,556
  • 5
  • 22
  • 37