Edit: Please note that this answer was asked before the question was completely redesigned. Because of this, he now refers to things that were present only in the question, as originally indicated. I apologize for all the "dangling pointers". :-)
Short answer:
, , IFoo<T>. , ( , ).
:
? , , ?
, factory :
var stringFoo = FooFactory.CreateFoo<string>();
(string ) , ( , ). , factory IFoo<string>.
, , :
var stringFoo = StringFoo.Create();
, , factory StringFoo, , :
public class StringFoo : IFoo<string>
{
...
public static StringFoo Create()
{
return new StringFoo();
}
}
IFoo<T>, if switch FooFactory.CreateFoo<T>, ( ).
, , factory, , ; , , , .
P.S.: , IoC. , , (, ) ; (, Autofac):
var builder = new ContainerBuilder();
builder.RegisterType<StringFoo>().As<IFoo<string>>();
:
using (var container = builder.Build())
{
var stringFoo = container.Resolve<IFoo<string>>();
...
}
Resolve - . , StringFoo. , !: -)