我在使用 Spring4D 框架的 GlobalContainer 创建从另一个继承的自定义类对象时遇到问题。
父类:
type
TVSPSection = class ( TInterfacedObject, IVSPSection )
private
_id : Integer;
_organization : IDirectoryObject;
function GetId () : Integer;
procedure SetId ( const value : Integer );
function GetOrganization () : IDirectoryObject;
procedure SetOrganization ( const value : IDirectoryObject );
public
property Id : Integer read GetId write SetId;
property Organization : IDirectoryObject read GetOrganization write SetOrganization;
end;
...
initialization
GlobalContainer.RegisterType<TVSPSection>.Implements<IVSPSection>.
InjectField ( '_organization' )
;
后代阶层:
type
TVSPSeismicSection = class ( TVSPSection, IVSPSeismicSection, IInterface )
private
_report : IReport;
function GetReport () : IReport;
procedure SetReport ( const value : IReport );
public
property Report : IReport read GetReport write SetReport;
end;
...
initialization
GlobalContainer.RegisterType<TVSPSeismicSection>.Implements<IVSPSeismicSection>.
InjectField ( '_report' )
;
end.
我尝试创建 TVSPSeismicSection 对象:
_seismicSection : IVSPSeismicSection;
_seismicSection := GlobalContainer.Resolve<IVSPSeismicSection>;
接下来,我尝试访问(父类的)“组织”字段并获得访问冲突错误。
_seismicSection.Organization.Id := -1; <- exception is here
那么,问题是如何告诉父类使用 GlobalContainer 解析器来启动它的字段?也许通过 DelegateTo 方法,但如何?
我发现的一种方法是在后代类的构造函数中启动父字段,如下所示:
constructor TVSPSeismicSection.Create ();
begin
Organization := GlobalContainer.Resolve<IDirectoryObject>;
end;
但这违反了依赖注入范式,因为我必须在后代类中包含其他类(IDirectoryObject)。