Spring4d - 具有服务名称的自动工厂
Spring4d - Automatic factory with service name
我正在尝试使用 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
我希望工厂在AServiceName = 'Service1'
时解决TMyComponent1
,AServiceName = 'Service2'
时TMyComponent2
,等等
我怎样才能做到这一点?
您目前不能。参数全部通过解析器用作潜在的构造函数参数。
能够注释工厂接口的参数以将其用作请求的服务类型可能值得一个功能请求。
然而,这不会很快在 1.3 中实现,我计划对系统进行一些更改,这将使上下文注入和注册成为可能。然后,这可能不仅可以通过其名称而且可以通过提供的任何可能的元信息来确定已解析的服务。
我正在尝试使用 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
我希望工厂在AServiceName = 'Service1'
时解决TMyComponent1
,AServiceName = 'Service2'
时TMyComponent2
,等等
我怎样才能做到这一点?
您目前不能。参数全部通过解析器用作潜在的构造函数参数。
能够注释工厂接口的参数以将其用作请求的服务类型可能值得一个功能请求。
然而,这不会很快在 1.3 中实现,我计划对系统进行一些更改,这将使上下文注入和注册成为可能。然后,这可能不仅可以通过其名称而且可以通过提供的任何可能的元信息来确定已解析的服务。