Hi David,

 

It's easy to force developers to call Dispose but your library calls must
use a particular signature.

 

Given an interface definition like so:

 

    public interface IPricingService : IDisposable

    {

        IEnumerable<IPrice> GetPrices(int @ref, DateTime date,
PricingProviders pricingProviders);

    }

 

Implement the service like this:

 

    public class PricingService : IPricingService

    {

        private PricingService();

 

        public static T Using<T>(Func<IPricingService, T> func)

        {

            using (IPricingService pricingService = new PricingService())

            {

                return func(pricingService);

            }

        }

 

        public IEnumerable<IPrice> GetPrices(int @ref, DateTime date,
PricingProviders pricingProviders) { ... }

 

        public void Dispose() { ... }

    }

 

Then your developers call the service like this:

 

    var prices = PricingService.Using(ps => ps.GetPrices(10, DateTime.Now,
PricingProviders.All));

 

The only way to access the service is through the static Using function that
calls dispose when the function is done.

 

You can still use closures to get at the IPricingService instance if need
be, but it will be disposed when the function finishes.

 

How's that?

 

Cheers.

 

James.

 

From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com]
On Behalf Of David Rhys Jones
Sent: Monday, 24 January 2011 23:08
To: ozDotNet
Subject: Forcing Dispose to be Called.

 


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