I'm trying to implement an identity map using generics. I have an abstract class, Entity, and a derivation constraint on my map for Entity. Since my map needs to be able to instantiate entities, my map also has a constructor constraint.
However, for the map to be useful, Entity subclasses should not be able to be instantiated from client code, which means I would want an internal constructor and no public constructors. This conflicts with the constructor constraint, though.
Is there something I'm missing? Is there some way of refactoring this to get the desired result?
The following code compiles as-is but, ideally, Entity's subclasses' constructors would be internal:
public abstract class Entity
{
public int Id { get; protected internal set; }
}
public sealed class Widget : Entity
{
// Client code should not be allowed to instantiate entities.
// But the constraints on EntityMap require that entities have
// a public constructor.
public Widget() { }
}
public sealed class Gadget : Entity
{
public Gadget() { }
}
// The new() constraint is required so that Get() can instantiate Ts.
public class EntityMap<T> where T : Entity, new()
{
private Dictionary<int, T> _entities = new Dictionary<int, T>();
private object _getLock = new object();
public T Get(int id)
{
lock (_getLock)
{
if (!_entities.ContainsKey(id))
_entities.Add(id, new T() { Id = id });
}
return _entities[id];
}
// Client code should not be allowed to instantiate maps.
internal EntityMap() { }
}
// Ideally, the client would only be able to obtain Entity
// references through EntityMaps, which are only accessible
// through the ApplicationMap.
public static class ApplicationMap
{
public static EntityMap<Widget> Widgets = new EntityMap<Widget>();
public static EntityMap<Gadget> Gadgets = new EntityMap<Gadget>();
}