4

Is it possible and if yes are there implementations of Dependency Injection containers in Delphi (Win32) which support "custom attribute" - based injection, something looking like

TDiExample = class(TObject)
private
  [Dependency]
  AOther: ISomeInterface

...
end;

(See Dependency Injection and .NET Attributes for a .Net counterpart, and https://stackoverflow.com/questions/812599/is-there-a-dependency-injection-framework-for-delphi-or-free-pascal for Delphi Win32 DI frameworks)

So far I have seen examples using direct calls to a dependency injection container, like:

var
  AOther: ISomeInterface
...
  AOther := DiContainer.Get(ISomeInterface) as ISomeInterface;
Community
  • 1
  • 1
mjn
  • 36,362
  • 28
  • 176
  • 378
  • 1
    Are you talking about the "standard" / "native Win32" Delphi, or about Delphi.NET ?? Delphi.NET certainly has those attributes - but that whole infrastructure is part of the .NET framework, so I highly doubt that the native Win32 Delphi would have that – marc_s May 29 '11 at 07:50
  • 3
    @mark_s: Delphi 2010 introduced Attributes and a reflection like Runtime type information (in the native compiler) – Andreas Hausladen May 29 '11 at 11:46
  • Many consider the use of attributes a design smell, since this couples the application to a particular DI container. Try to find a container that is flexible enough to prevent you from having to use attributes and design your application accordingly. – Steven May 30 '11 at 10:44
  • @Steven I don't see why attributes are worse than _direct_ calls to _unstandardized_ DI framework methods which require a _specific_ unit to appear in the uses list. – mjn May 30 '11 at 11:25
  • @mjn: Using attributes is *not* worse than direct calls to a DI framework. They are both bad. Your application should be ignorant of any DI framework. Configure the container in the startup path of the application and resolve everything from there. This will make your application design very clean, makes it much easier to unit test and makes it easier to migrate to another framework when needed. – Steven May 30 '11 at 11:41
  • @Steven not everything can be done in the startup phase. Container configuration should happen only there, I agree. But how can I do the actual dependency injection (instead of a '.Create') at a later time without any DI code - either calling some container method or using attributes (like Google Guice)? Direct binary code manipulation is a no-no for me. – mjn May 30 '11 at 13:20
  • @mjn: Resolving something at later time depends a bit on the framework you're working on, but the general principle is to use a factory interface. The implementation of that interface will know about the container and is allowed to call it directly. – Steven May 30 '11 at 13:54

1 Answers1

5

Yes it is possible. My simple DI container will can do this in the next release and there will be many others improvements too. See the current version here https://github.com/danieleteti/delphidicontainer

coding Bott
  • 4,287
  • 1
  • 27
  • 44
Daniele Teti
  • 1,764
  • 14
  • 20