How can I make this simple C# generics factory work?

Posted by Kevin Brassen on Stack Overflow See other posts from Stack Overflow or by Kevin Brassen
Published on 2010-05-19T15:55:21Z Indexed on 2010/05/19 16:00 UTC
Read the original article Hit count: 195

Filed under:
|
|

I have this design:

public interface IFactory<T> {
  T Create();
  T CreateWithSensibleDefaults();
}

public class AppleFactory : IFactory<Apple> { ... }
public class BananaFactory : IFactory<Banana> { ... }
// ...

The fictitious Apple and Banana here do not necessarily share any common types (other than object, of course).

I don't want clients to have to depend on specific factories, so instead, you can just ask a FactoryManager for a new type. It has a FactoryForType method:

IFactory<T> FactoryForType<T>();

Now you can invoke the appropriate interface methods with something like FactoryForType<Apple>().Create(). So far, so good.

But there's a problem at the implementation level: how do I store this mapping from types to IFactory<T>s? The naive answer is an IDictionary<Type, IFactory<T>>, but that doesn't work since there's no type covariance on the T (I'm using C# 3.5). Am I just stuck with an IDictionary<Type, object> and doing the casting manually?

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics