Home » Delphi 2010 » Delphi Dependency Injection » Design Patterns » A simple Dependency Injection Container for Delphi

A simple Dependency Injection Container for Delphi

As wikipedia says:

“Dependency injection (DI) in computer programming refers to the process of supplying an external dependency to a software component. It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency. The term was first coined by Martin Fowler to describe the mechanism more clearly.

Many of us have already read this historical article from Martin Fowler about dependency injection pattern, but actually there isn’t a real framework for implement dependency injection in Delphi.

There are already the following implementation for DI in Delphi

  • Emballo (work with pre-D2010 too, but the implementation require changes in the service classes. I really hate it)
  • Delphi Spring Framework (very nice, but still not realeased)

So, I decided to write my own simple DI framework.

You can find the code at google code project here: http://code.google.com/p/delphidicontainer/

This is the first public version and come with sample, documentation and unit tests.

Folow some sample code.

program Test01;
{$APPTYPE CONSOLE}

uses
  SysUtils,
  DIContainer in '....srcDIContainer.pas',
  ServiceTestObjectsU in '....UnitTestServiceTestObjectsU.pas';

var
  DIContainer: TDIContainer;
  s1: TService1;
  s2: TService2;
  s3: TService3;
  s6: TService6;
  s7: TService7;
begin
  try
    DIContainer := TDIContainer.Create;
    try
      // AddComponent with TClass with and   InitType = Singleton
      DIContainer.AddComponent(TService1, TDIContainerInitType.Singleton);
      // AddComponent with QualifiedName and InitType = Singleton
      DIContainer.AddComponent('ServiceTestObjectsU.TService2',
        TDIContainerInitType.Singleton);
      // AddComponent with QualifiedName and InitType = CreateNewInstance
      DIContainer.AddComponent('ServiceTestObjectsU.TService3',
        TDIContainerInitType.CreateNewInstance);

      // GetComponent with QualifiedName
      s1 := DIContainer.GetComponent('ServiceTestObjectsU.TService1')
        as TService1;
      s1.Message := 'I''m the first message';
      WriteLn(s1.Message);

      // GetComponent with TClass
      s2 := DIContainer.GetComponent(TService2) as TService2;
      s2.Message := 'I''m the second message';
      WriteLn(s2.Message);

      // GetComponent with a dependent service (TService3 depends upon TService1 and TService2)
      s3 := DIContainer.GetComponent('ServiceTestObjectsU.TService3')
        as TService3;
      WriteLn(s3.GetCompoundMessage);
      // s3 is not created as Singleton, so after use it I must free it
      s3.Free;

      // AddComponent with QualifiedClassName, a custom initializer, an alias.
      // Component will be created as singleton (single instance managed by Container)

      DIContainer.AddComponent(DIContainerUtils.GetQualifiedClassName
          (TService6),
          function: TObject
          begin
            Result := TService6.Create(DIContainer.Get(TService1) as TService1,DIContainer.Get(TService1) as TService1);
          end,
          'srv6',
        TDIContainerInitType.Singleton);

      s6 := DIContainer.Get('srv6') as TService6;
      WriteLn(s6.ToString);
      s6 := DIContainer.Get('srv6') as TService6;
      WriteLn(s6.ToString);

      // AddComponent with QualifiedClassName, a custom initializer, an alias.
      // Component will be created as singleton (single instance managed by Container)
      DIContainer.AddComponent(DIContainerUtils.GetQualifiedClassName
          (TService7),
            function: TObject
            begin
              Result := TService7.Create(DIContainer.Get(TService1) as TService1,DIContainer.Get(TService1) as TService1);
            end,
            'srv7intf',
          TDIContainerInitType.Singleton);

      s7 := DIContainer.Get('srv7intf') as TService7;
      WriteLn(s7.ToString);
    finally
      DIContainer.Free;
    end;
  except
    on E: Exception do
      WriteLn(E.ClassName, E.Message);
  end;
  readln;
end.

DelphiDIContainer also handle constructor injection with automatic dependency resolution for service.

Enjoy and stay tuned.

10 commenti a “A simple Dependency Injection Container for Delphi

  1. Thanks, Daniele.
    It seems that your implementation will satisfy the basic requirements.

    Since this project is created based on the Delphi 2010 platform. It’ll be great if you consider support for Generics/Attributes which is more natural.

    btw. We’re preparing for the first public release of the Delphi Spring Framework. You’re welcome to give any feedback :)

  2. very interesting, i’m not using delphi a lot today but I was looking at a DIC at the time.
    is it possible to port this to freepascal?
    thanks

  3. @paul:
    Sure I’m planed to support generics and attributes.
    Thanks for the tip

    @devsmt:
    delphidicontainer use a lot the new RTTI. I dont know how RICH is the freepascal RTTI. If you want to try I can help you.

  4. Hi, Daniele. I’m the creator of Emballo..
    I’ve recreated it from scratch, now based on D2010 (older Delphi versions won’t be supported) and I’m already using it on an a commercial product I’m developing.
    It isn’t available for public now, but I plan to release this new version on Google Code in a couple of days.
    Could you tell me what didn’t you like on it? I’d be pleased to get your suggestions

  5. @Magno Machado
    I’m looking for new version of Emballo.
    The last version doesn’t support DI with contructor injection (delphidicontainer support only this kind of injection) and require a change in the service constructor.
    Thanks for your work.

  6. This is because of the poor reflection capabilities of delphi pre-2010, but the new version does support constructor injection pretty well… I think you’ll like it :)

    As soon as I release it, I’ll leave a notice here

  7. I’m glad to announce that I’ve just checked in the new version of Emballo.
    The main improvements are the constructor injection, and the ability to manually ask the framework for an instance of a specific interface, like your DIContainer.GetComponent function. In the previous version, injections were always done on instance fields of objects.

    There is no requirements for the services classes, they doesn’t need to implement any framework specific interface or inherit from some class. The only restriction is that services must be interfaces. Although technically it’s not necessary, I think this is a good practice and I decided to force it.

    This new version is on the same project page as the previous was.

  8. Pingback: Kylie Batt
  9. Pingback: E-bike

Lascia una risposta

L'indirizzo email non verrĂ  pubblicato. I campi obbligatori sono contrassegnati *

È possibile utilizzare questi tag ed attributi XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>