Return to Snippet

Revision: 24557
at March 3, 2010 17:17 by streamt


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

namespace Services
{
    public interface IService
    {
        
    }

    public interface IServiceProvider<T>
    {
        T GetService<T>();
        object GetService(Type serviceType);
    }

    public sealed class ServiceProvider : IServiceProvider<IService>
    {
        private Dictionary<Type, object> _services;

        private ServiceProvider()
        {
            _services = new Dictionary<Type, object>();
        }

        public void RegisterService(Type type, object service)
        {
            if (!_services.ContainsKey(type))
            {
                _services.Add(type, service);
            }
        }

        public T GetService<T>() where T : IService
        {
            return (T)_services[typeof(T)];
        }

        public object GetService(Type serviceType)
        {
            return (_services.ContainsKey(serviceType)) ? _services[serviceType] : null;
        }
    }
}
}

Initial URL

                                

Initial Description

                                

Initial Title
Service Provider

Initial Tags
service

Initial Language
C#