我想了解委托工厂模式与Autofac。我知道如何使用IIndex <>与健()注册,这是在这里很好地解释实施工厂:Configuring an Autofac delegate factory that's defined on an abstract classAutofac委托工厂使用func <>
我想知道如果我能创建使用Func键<>工厂,我会怎么做以下示例注册:
public enum Service
{
Foo,
Bar
}
public interface FooService : IService
{
ServiceMethod();
}
public interface BarService : IService
{
ServiceMethod();
}
public class FooBarClient
{
private readonly IService service;
public FooBarClient(Func<Service, IService> service)
{
this.service = service(Service.Foo);
}
public void Process()
{
service.ServiceMethod(); // call the foo service.
}
}
你为什么不只是使用'KeyIndex <>'带'Keyed()'? Autofac无法为你创建这个'Func'。您需要使用'Keyed()'或'Named()'类似以下方式将其注册到容器中:'builder.Register >(c => s => c.ResolveKeyed (s) );'委托工厂只能创建一个带参数的类型,而不能根据参数选择一个类型,因为这就是'IIndex <>'的用途。 –
nemesv
2013-03-15 07:26:59
对于IIndex <>我将需要引用我试图避免的Autofac库。我希望我的DI代码只能在可能的情况下在Composite根目录(单独的库)中。 – 2013-03-15 08:10:35