using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreLibrary.Util
{
    public class ServiceLocator
    {
        #region Fields

        private static Dictionary<Type, object> serviceList = new Dictionary<Type, object>();

        #endregion

        #region Methods

        public static void Add<T, U>(U service) where U : T
        {
            if (serviceList.ContainsKey(typeof(T)))
                throw new ArgumentException(string.Format("Implementation of '{0}' is already added.", typeof(T)));

            serviceList.Add(typeof(T), service);
        }

        public static T Resolve<T>()
        {
            if (!serviceList.ContainsKey(typeof(T)))
                throw new ArgumentException(string.Format("Implementation for '{0}' hasn't been added.", typeof(T)));

            return (T)serviceList[typeof(T)];
        }

        #endregion
    }
}
