Hi David,

You can't really force developers to call dispose but here's a couple of
ideas.

a) If appropriate use the dispose pattern
http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=VS.90).aspx This is one
of those cases where you actually have an external resource to manage so
this is totally appropriate

b) Don't make them do it. If you are injecting the IPricingService (as it
seems you are from the comments) then find a way to manage the lifetime of
the service externally to the calling code. Not always the easiest thing
especially in a desktop app where you often don't have clear "session"
boundaries

c) Don't give them an IPricingService give them an IPricingServiceFactory.
If they have to create their own IPricingService they are far more likely to
think of disposing of it properly.

d) A variation on (b), force the user to create a "session" which is itself
an IDisposable object that will handle the connection and cleanup code.
Ensure that there is a session before executing any calls. You could move
the calls which actually do things into this new session object but then you
are really just doing (b) with different names. This would be one of my
preferred options and looks like this:

 // Spring.Net
 public IPricingService PricingService { private get; set; }

 public IEnumerable<IPrice> GetPrices(int ref, DateTime date)
 {
    using(var session = PricingService.StartSession())
    {
      return session.GetPrices(ref,date,PricingProviders.All);
    }
 }

Note that this really doesn't stop them from calling StartSession outside of
a using block and then never calling dispose on it.

e) Handle the timer/cleanup stuff on the server. They have the limited
resource, if someone is hogging it they should deny that caller. They'll
hopefully get the message eventually and fix their code.


Hope one of those helps you out :)

Regards,
--
Michael M. Minutillo
Indiscriminate Information Sponge
Blog: http://wolfbyte-net.blogspot.com


On Mon, Jan 24, 2011 at 8:38 PM, David Rhys Jones <[email protected]>wrote:

>
> Hi all,
>
> Background:
>
>  I've got a service connection that is limited to 5 logins, (internal app
> written in another dept, Java Webservice not WCF compatible.),  I've
> implemented a library (C# 3.5) that calls this and a number of different
> services to provide a coherent data source for our Excel (2003)
> applications.
> In my dispose, I clean everything up correctly, connections are closed,
> webservice is disposed etc.
>
> Problem:
>
>  I need to find a way to force the developpers in my team and in the other
> teams that use my library to call the Dispose method after each use of the
> library.
>
> Is there a pattern or does anyone have an idea, how I can put a limit on
> how long the object can be used for?
> I can think of some really kludgy ways using elapsed time and throwing an
> exception.
>
>
>
> Examples:
>
>
> Good : all calls are closed, webservice is disposed, sockets closed etc.
>
>  // Spring.Net
>  public IPricingService PricingService { private get; set; }
>
>  public IEnumerable<IPrice> GetPrices(int ref, DateTime date)
>  {
>             using (PricingService)
>             {
>                 return
> PricingService.GetPrices(ref,date,PricingProviders.All);
>             }
> }
>
>
> Bad : object is not closed, open sockets and the login used for this call
> will block until the webservice timeout.
>
>  // Spring.Net
>  public IPricingService PricingService { private get; set; }
>
>  public IEnumerable<IPrice> GetPrices(int ref, DateTime date)
>  {
>                  return
> PricingService.GetPrices(ref,date,PricingProviders.All);
>   }
>
>
> thanks
> Davy,
>
>
> "Always code as if the guy who ends up maintaining your code will be a
> violent psychopath who knows where you live."
> - Martin Golding
>
>

Reply via email to