using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Wayne.ForecourtControl.Fusion
{
    public static class CachingXmlSerializer
    {
        private static readonly Dictionary<Type, XmlSerializer> Cache = new Dictionary<Type, XmlSerializer>();

        private static readonly object SyncRoot = new object();

        public static XmlSerializer Create<T>()
        {
            var type = typeof (T);

            if (type == null) throw new ArgumentNullException("type");            

            lock (SyncRoot)
            {
                if (!Cache.ContainsKey(type))
                {
                    Cache.Add(type, new XmlSerializer(type));
                }
            }

            return Cache[type];
        }     
    }
}