我正在尝试使用Spring4d的自动虚拟工厂功能。不过,我希望能够在工厂Build()函数中传递我想要解析的ServiceName。像这样:AFactory.Build(AServiceName)
例如
TMyComponent1 = class(TInterfacedObject, IMyService)
public
constructor Create(AArgument : TObject);
end;
TMyComponent2 = class(TInterfacedObject, IMyService)
public
constructor Create(AArgument : TObject);
end;
TMyComponent3 = class(TInterfacedObject, IMyService)
public
constructor Create(AArgument : TObject);
end;
// Registering components
AContainer.RegisterType<TMyComponent1, IMyService>('Service1');
AContainer.RegisterType<TMyComponent2, IMyService>('Service2');
AContainer.RegisterType<TMyComponent3, IMyService>('Service3');
// Factory interface
IMyFactory = class(IInvokable)
[Guid]
function Build(AArgument : TObject; AServiceName : string) : IMyService;
end;
// Factory registration
AContainer.RegisterType<IMyFactory>.AsFactory();
// Use factory
AContainer.Resolve<IMyFactory>(AObject, 'Service1'); // Should resolve TMyComponent1我希望工厂能解决AServiceName = 'Service1'时的TMyComponent1,AServiceName = 'Service2'时的TMyComponent2等。
我怎么才能得到这个呢?
发布于 2018-03-06 00:20:06
目前还不能。所有参数都通过解析器作为潜在的构造函数参数使用。
能够注释工厂接口的参数以将其用作所请求的服务类型,这可能是值得的功能请求。
然而,这不会很快实现,因为对于1.3,我计划对系统进行一些更改,这将使上下文注入和注册成为可能。这不仅可以通过其名称,而且可以通过所提供的任何可能的元信息来确定所解析的服务。
https://stackoverflow.com/questions/49113772
复制相似问题