我正在尝试使用Spring4d
. 不过,我希望能够在工厂函数中传递ServiceName
我想要解决的问题。Build()
像这样: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
我希望工厂解决TMyComponent1
when AServiceName = 'Service1'
,TMyComponent2
whenAServiceName = 'Service2'
等。
我怎样才能做到这一点?