inheritance - How to create inherited object using GlobalContainer (Spring4D framework) in Delphi? -
i have problem in creating custom class object inherited using globalcontainer of spring4d framework.
parent class:
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' ) ;
descendant class:
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.
and attempt create object of tvspseismicsection:
_seismicsection : ivspseismicsection; _seismicsection := globalcontainer.resolve<ivspseismicsection>;
next, try access 'organization' field (of parent class) , access violation error.
_seismicsection.organization.id := -1; <- exception here
so, question how tell parent class initiate fields using globalcontainer resolver? maybe via delegateto method, how?
one way have found initiate parent fields in descendant class's constructor this:
constructor tvspseismicsection.create (); begin organization := globalcontainer.resolve<idirectoryobject>; end;
but violates dependency injection paradigm, because have include additional classes (idirectoryobject) descendant class.
there several ways , depends best. dependencies required should passed constructor arguments , optional should passed property injection.
so if add constructor taking idirectoryobject
argument tvspsection
class container able satisfy argument if knows how resolve idirectoryobject
.
that cleanest way 1 rule using di container write code still di container agnostic , use container tool not dependency.
if want keep property injection using inject
attribute on property should work (don't forget add spring.container.common
uses attribute) specifying injectproperty in registration (while possible avoid field injection doable rtti , not regular code - see previous paragraph).
Comments
Post a Comment