using System;
using System.Collections.Generic;

namespace Diametric.Service
{
        /// <summary>
        /// An implementation of IServiceProvider that uses a dictionary
        /// </summary>
        public class DictionaryServiceProvider:IServiceProvider
        {
                private Dictionary<Type, object> services = new Dictionary<Type,object>();

                public DictionaryServiceProvider()
                {
                }

                #region IServiceProvider Members

                /// <summary>
                /// Returns the specified service or null if the service doesn't exist.
                /// </summary>
                /// <param name="serviceType"></param>
                /// <returns></returns>
                public object GetService(Type serviceType)
                {
                        object service = null;
                        services.TryGetValue( serviceType, out service );
                        return service;
                }

                #endregion

                #region Public API

                /// <summary>
                /// Adds a service to the service provider's list of services using the object's type as the key.
                /// </summary>
                /// <param name="service"></param>
                public void AddService(object service)
                {
                        services.Add( service.GetType(), service );
                }

                /// <summary>
                /// Adds a service to the service provider's list of services using the provided Type as the key.
                /// </summary>
                /// <param name="service"></param>
                public void AddService(object service, Type key)
                {
                        services.Add( key, service );
                }

                #endregion
        }
}