Service locator for a 2D game I'm currently developing a simple 2D game engine and I was just wondering, what are some things that I can do to improve my service locator class, at the moment it's very basic.

For example, should I be checking for null objects, if so, what exceptions should I throw?

Here's the code, thatnks in advance!

using System;
using System.Collections.Generic;

namespace GameEngine.Core
{
    public sealed class ServiceLocator
    {
        private static ServiceLocator instance;

        private Dictionary&lt;Type, object&gt; services;

        public ServiceLocator()
        {
            this.services = new Dictionary&lt;Type, object&gt;();
        }

        public void AddService(Type serviceType, object serviceObject)
        {
            this.services.Add(serviceType, serviceObject);
        }

        public void RemoveService(Type serviceType)
        {
            this.services.Remove(serviceType);
        }

        public object GetService(Type serviceType)
        {
            return this.services[serviceType];
        }

        public static ServiceLocator Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ServiceLocator();
                }

                return instance;
            }
        }
    }
}
I think you are reinventing the wheel a bit too much.  If you need something simple then go with built in .net ones.  If you going to make a simple ServiceLocator then you should implement the IServiceContainer interface.  

public sealed class ServiceLocator : IServiceContainer


This is a matter of preference but I usually set static instance property in a static constructor.  There isn't a lot of overhead in creating the object right away that we need to do locking or delayed creation when they access the property. 

public static IServiceContainer Instance { get; }

static ServiceLocator()
{
    Instance = new ServiceLocator();
}


Now I would create two constructors one the default and one that takes a IServiceContainer

private readonly IServiceContainer _container;

public ServiceLocator() : this(new ValidationContext(string.Empty).ServiceContainer)
{
}

public ServiceLocator(IServiceContainer container)
{
     if (container == null)
     {
         throw new ArgumentNullException(nameof(container));
     }
    _container = container;
}


I typically use the Servicecontainer from the ValidationContext class.  It's marked internal so we can not just new it up.  The difference between the ServiceContainer class and the one from the ValidationContext is two fold.  One the ServiceContainer class implements IDisposable and two it caches the results from the ServiceCreatorCallback delegate.   It would be up to you to decide which one of the ServiceContainers from .net you would prefer. 

The rest of the methods I would just chain down to the container we set in the constructor

public object GetService(Type serviceType)
{
    return _container.GetService(serviceType);
}

public void AddService(Type serviceType, object serviceInstance)
{
    _container.AddService(serviceType, serviceInstance);
}

public void AddService(Type serviceType, object serviceInstance, bool promote)
{
    _container.AddService(serviceType, serviceInstance, promote);
}

public void AddService(Type serviceType, ServiceCreatorCallback callback)
{
    _container.AddService(serviceType, callback);
}

public void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
{
    _container.AddService(serviceType, callback, promote);

}

public void RemoveService(Type serviceType)
{
    _container.RemoveService(serviceType);
}

public void RemoveService(Type serviceType, bool promote)
{
    _container.RemoveService(serviceType, promote);
}


This is a very simple Service Locator.   There are lots of very good dependency injection library out there but this will work if you only need a simple container.