I have say a dozen types T
which inherit from EntityObject
and IDataObject
.
I have generic the following interface
IDataMa
Here is a neat solution if having the locator as singleton is fine:
static class Locator
{
private static class LocatorEntry where T : ...
{
public static IDataManager instance;
}
public static void Register(IDataManager instance) where T : ...
{
LocatorEntry.instance = instance;
}
public static IDataManager GetInstance() where T : ...
{
return LocatorEntry.instance;
}
}
If you cannot implement the locator as singleton, I believe there is no there around creating a Dictionary
class Locator
{
private readonly Dictionary instances;
public Locator
{
this.instances = new Dictionary();
}
public void Register(IDataManager instance) where T : ...
{
this.instances[typeof(T)] = instance;
}
public IDataManager GetInstance() where T : ...
{
return (IDataManager)this.instances[typeof(T)];
}
}