I would love to be able to code in Delphi this way, simply annotating a field:
type
TMyClass = class
private
[Inject]
Factory: ISomeFactory;
...
end;
or by attributing a setter
type
TMyClass = class
private
FFactory: ISomeFactory;
[Inject]
procedure SetFactory(const AFactory: ISomeFactory);
...
public
property Factory: ISomeFactory read FFactory write SetFactory;
end;
The background: I am moving old code to a service-oriented architecture and find that referencing the service layer always leads to constructs like
DataModule1.ServiceLayerInstance1.SubSystemN.InvokeSomething(Params, ...);
which could be much shorter like
type
Form1 = class(TForm1)
private
[Inject]
SubsystemN: ISubsystemN;
...
end;
...
SubsystemN.InvokeSomething(Params, ...);
You can achieve this goal with the Emballo OpenSource project.
See the project on Google Code: http://code.google.com/p/emballo/wiki/WhyDependencyInjection
Yes, there is. The Delphi Spring Framework
http://www.spring4d.com/
does precisely this. It has an [Inject] attribute.
One caveat -- to use it, you need to include the Spring unit in your code where the attribute is defined. Otherwise, the compiler will ignore the attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With