Managing rendering targets in XNA using IDisposable I came up with the following class that I can use as an XNA service to manage rendering to things other than the screen as I didn't want to be passing Games/GraphicsDevices around all over the place for testing reasons. I can't see anything wrong with it, but I'm curious what other people think about the design/etc. of it and the general theory of it. Is this abusing IDisposable, or is this an ok way to do things?

Typical Usage:

using(Services.GetService&lt;IRenderTargetController&gt;().SetRenderTarget(renderTarget, Color.Black)) {
    // [snip] Render stuff as normal here.
}


Implementation:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

sealed class RenderTargetController : IRenderTargetController
{
    private readonly GraphicsDevice _graphicsDevice;
    private readonly Stack&lt;RenderTarget2D&gt; _targets;

    public RenderTargetController(GraphicsDevice graphicsDevice)
    {
        _graphicsDevice = graphicsDevice;
        _targets = new Stack&lt;RenderTarget2D&gt;();
    }

    public IDisposable SetRenderTarget(RenderTarget2D target)
    {
        return SetRenderTarget(target, Color.Transparent);
    }

    public IDisposable SetRenderTarget(RenderTarget2D target, Color resetColor)
    {
        _targets.Push(target);
        _graphicsDevice.SetRenderTarget(target);
        _graphicsDevice.Clear(resetColor);

        return new TargetManager(this);
    }

    private class TargetManager : IDisposable
    {
        private readonly RenderTargetController _controller;

        public TargetManager(RenderTargetController controller)
        {
            _controller = controller;
        }

        public void Dispose()
        {
            _controller._targets.Pop();

            RenderTarget2D target = null;
            if (_controller._targets.Count &gt; 0)
            {
                target = _controller._targets.Peek();
            }

            // Setting render target to null returns control to the screen.
            _controller._graphicsDevice.SetRenderTarget(target);
        }
    }
}
An approach itself is fine IMO, but the API is misleading. Why would SetXXX method ever return something, let alone return an IDisposable? Maybe come up with a better name.