December 1, 2012

Singleton Access from a Container

I learned a cool programming technique this week from a co-worker Kelly Brownsberger to enable container access to a singleton. Say you have an interface IApplicationSettings exposed as a static singleton property, but you want to inject it into other classes via a container. The trick is to create another class that implements IApplicationSettings and grabs a reference to the singleton in the constructor. Each implementation of an interface member simply delegates the call to the corresponding member of the singleton reference. Now you have a class that can be registered with the container and gets its values from the singleton.

    public interface IApplicationSettings

    {

        bool AutoRefresh { get; }

        bool DisplayShoppingCart { get; }

    }



    /// <summary>

    /// Singleton implementation of IApplicationSettings.

    /// </summary>

    public class ApplicationSettings : IApplicationSettings

    {

        private static IApplicationSettings _current;



        public static IApplicationSettings Current

        {

            get

            {

                if (_current == null)

                    _current = ReadSettings();



                return _current;

            }

        }



        private static IApplicationSettings ReadSettings()

        {

            // Settings would actually come from a config file or DB.

            return new ApplicationSettings(true, true);

        }



        private ApplicationSettings(bool autoRefresh, bool displayShoppingcart)

        {

            AutoRefresh = autoRefresh;

            DisplayShoppingCart = displayShoppingcart;

        }



        public bool AutoRefresh { get; private set; }



        public bool DisplayShoppingCart { get; private set; }

    }



    /// <summary>

    /// Container-friendly or injectable instance of IApplicationSettings.

    /// Delegates to the singleton.

    /// </summary>

    public class ApplicationSettingsInstance : IApplicationSettings

    {

        private readonly IApplicationSettings _instance;



        public ApplicationSettingsInstance()

        {

            _instance = ApplicationSettings.Current;

        }



        public bool AutoRefresh

        {

            get { return _instance.AutoRefresh; }

        }



        public bool DisplayShoppingCart

        {

            get { return _instance.DisplayShoppingCart; }

        }

    }

This is a useful technique, but I don’t like the way each implementation of an interface member in the ApplicationSettingsInstance class has to delegate to the private instance field. I’ve always thought it would be nice if C# had a feature where you could specify that a particular interface is implemented by a private member. Then the C# compiler could automatically fill in the interface implementation with calls to the private member. The syntax could look something like the example below. The _instance field implements the interface, and the interface members go away.

    /// <summary>

    /// Container-friendly or injectable instance of IApplicationSettings.

    /// Delegates to the singleton.

    /// </summary>

    public class ApplicationSettingsInstance : IApplicationSettings

    {

        private readonly IApplicationSettings _instance

            handles IApplicationSettings;



        public ApplicationSettingsInstance()

        {

            _instance = ApplicationSettings.Current;

        }

    }

I’m not sure if any other languages have a feature like this. It seems like it would be fairly simple to implement. In the meantime, I’m adding it to my fantasy feature list.

© Joe Buschmann 2020