Spring4D Framework の 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>;
次に、(親クラスの)「Organization」フィールドにアクセスしようとすると、アクセス違反エラーが発生します。
_seismicSection.Organization.Id := -1; <- exception is here
問題は、親クラスに GlobalContainer リゾルバーを使用してフィールドを開始するように指示する方法です。多分 DelegateTo メソッド経由ですが、どうやって?
私が見つけた1つの方法は、次のように子孫クラスのコンストラクターで親フィールドを開始することです:
constructor TVSPSeismicSection.Create ();
begin
Organization := GlobalContainer.Resolve<IDirectoryObject>;
end;
しかし、これは依存性注入パラダイムに違反しています。追加のクラス (IDirectoryObject) を子孫クラスに含める必要があるからです。