Question

I am working with Active Directory using C#. Instantiating the PrincipalContext object seems to be expensive, so I'd like to store one in a class variable.

When using PrincipalContext as a local variable, I can use the convenient using syntax. When storing an IDisposable object in a static variable, how do I ensure the object is properly disposed of?

Was it helpful?

Solution

The general pattern for this is to implement the IDisposable interface on your class. Take this example:

public class YourClass : IDisposable
{
    private OtherDisposableType yourResource;

    public YourClass()
    {
        yourResource = new OtherDisposableType();
    }

    public void Dispose()
    {
        yourResource.Dispose();
    }
}

This is, at a minimum, what you need to do.

EDIT

My previous version advocated following the finalizer pattern in all cases, which was (correctly) pointed out to be against the framework design guidelines. However, in the event that you're actually dealing with unmanaged resources (for example, you're making direct P/Invoke calls and obtaining a handle that needs to be explicitly freed) it's advisable that you create a finalizer and call Dispose within it to protect against people who consume your code and don't call Dispose:

public class YourClass : IDisposable
{
    private OtherDisposableType yourResource;

    public YourClass()
    {
        yourResource = new OtherDisposableType();
    }

    public void Dispose()
    {
        yourResource.Dispose();

        GC.SuppressFinalize(this);
    }

    ~YourClass()
    {
        Dispose();
    }
}

OTHER TIPS

Look at what the System.ComponentModel namespace does. Basically, the pattern I typically use is to have a collection of subcomponents, which includes everything I own that's not a 'value' - whether or not it implements IDisposable.

Then, when I Dispose() myself, I iterate over this collection and Dispose anything that implements IDisposable.

One advantage of this technique is that if an object I own starts out not being Disposable, but later adds the IDisposable interface, my class will do the right thing without ever having to change.

Also, use of a DI/IoC container can handle much of this for you.

So basically you want to cache an expensive resource. That's a good thing.

Global data (static variables in this case) is not such a good thing, IMHO. Instead, why not make it an instance variable and control the lifetime?

Write your class that handles the AD responsibilities, have it create and use the PrincipalContext, and make it IDisposable as well (using the Dispose Pattern). Extract an interface from it to decouple it and make classes that use it easier to test.

Classes that want to use AD services will take a constructor parameter of your new interface (Dependency Injection or DI). You can either create your class manually in a using block and pass it to the classes or use a DI Container Framework. You can have the framework set the lifetime of the AD object to the lifetime of the container (which may also be IDisposable). See How do you reconcile IDisposable and IoC? and your DI Container documentation for more on this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top