Assuming the clients are not instantiating the Repository<T>
and you are providing the instances, you could pass a Func<T>
to do the activation for you.
public class Repository<T> {
private Func<T> _activator;
internal Repository<T>(Func<T> activator) {
_activator = activator;
}
public T GetById<T>() {
return _activator();
}
}
You would then create your Repository<T>
internally.
EntityRepository = new Repository<Entity>(() => new Entity());
Another advantage here is that if some entities have a non-empty .ctor, your Func<T>
can accommodate that.