.DelegateTo not available any longer In Spring4D?

237 views
Skip to first unread message

Pio Pio

unread,
Feb 3, 2024, 5:51:07 PMFeb 3
to Spring4D
Hello,

Another question, sorry. Am I wrong or the construct "Container.RegisterType<IMyInterface, TMyClass>.DelegateTo(" is not available any longer.
If so what this is replaced with ?

Many thanks
Alberto

Adam Siwoń

unread,
Feb 4, 2024, 2:28:28 PMFeb 4
to Spring4D
You could use:
Container.RegisterType<TMyClass>(ActivatorDelegate).Implements<IMyInterface>;

Anders Gustavsson

unread,
Feb 4, 2024, 4:41:57 PMFeb 4
to spri...@googlegroups.com
Not needed at all. Just put the function there! 

--
You received this message because you are subscribed to the Google Groups "Spring4D" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spring4d+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/spring4d/ab907aa3-bd5b-4a9c-beb0-9ba7043d7375n%40googlegroups.com.

Alberto Paganini

unread,
Feb 4, 2024, 4:59:35 PMFeb 4
to spri...@googlegroups.com
Hi Both and thank you for your answer, firstly.

Hi Anders,

If I use Container.RegisterType<TMyClass>; how can I link TMyClass to IMyInterface?

Hi Adam,

I have amended ActivatorDelegate into TActivatorDelegate but I get the following error
Container.RegisterType<TMyClass>(TActivatorDelegate).Implements<IMyInterface>;
[DCC Error] uRegistrations.pas(106): E2232 Interface 'TActivatorDelegate' has no interface identification

Where am I doing wrong here?

Many thanks
Alberto

You received this message because you are subscribed to a topic in the Google Groups "Spring4D" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spring4d/qsZAAALc-wQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spring4d+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/spring4d/CAEwXdwptfPvzoN2BmRy2zDxRcwZs_Prc8%3DW2qnCAZaNBMZFzBA%40mail.gmail.com.

Adam Siwoń

unread,
Feb 4, 2024, 5:12:07 PMFeb 4
to Spring4D
Hi,

I have no idea what is wrong in your code. Here is small example which works in my code:

type
  IMyInterface = interface(IInvokable)
    ['{419BF7C5-A87F-4FC9-AB37-2D475B1A5E6D}']
    procedure Test;
  end;

  TMyClass = class(TInterfacedObject, IMyInterface)
  public
    procedure Test;
  end;

procedure TMyClass.Test;
begin
end;

function Create_TMyClass: TMyClass;
begin
  Result := TMyClass.Create;
end;

procedure RegisterInContainer;
begin
  GlobalContainer.RegisterType<TMyClass>(Create_TMyClass).Implements<IMyInterface>;
  GlobalContainer.Build;
end;

Anders Gustavsson

unread,
Feb 5, 2024, 3:55:32 AMFeb 5
to spri...@googlegroups.com
Container.RegisterType<IMyIntetface, TMyClass>
.function: TMyClass 
begin 
  Result :=.... 
end;

Dominik Vorreiter

unread,
Feb 6, 2024, 8:20:26 AMFeb 6
to Spring4D
Stefan Glienke answered it here: https://groups.google.com/g/spring4d/c/AHG1Et8rWgw
You can pass the delegate directly to the RegisterType<>() function as an argument.

Pio Pio

unread,
Feb 7, 2024, 5:33:50 PMFeb 7
to Spring4D
Hello everybody and thanks for your answers.

What if I had the following construct

  Container.RegisterType<IMyInterface, TMyClass>('ABC')(
    function:  TMyClass
    begin
      result :=  TMyClass.Create
    end);

What is the new syntax?

Many thanks again
Alberto

Pio Pio

unread,
Feb 9, 2024, 5:12:37 PMFeb 9
to Spring4D
Hello,

I made a few attempts  and  the following code compiles:

Container.RegisterType<IMyInterface, TMyClass>('ABC').AsCustom(

function: TMyClass
begin
result := TMyClass.Create
end
);


but, is it correct?

Many thanks
Alberto

Pio Pio

unread,
Feb 11, 2024, 7:40:12 AMFeb 11
to Spring4D
Hello,

One more step.
I have updated the syntax of the several Container.RegisterType I have in the registration file as per my previous message and the application compiles.

I ran it and I got the error message "EcontainerException Container has changed registration information - Build required" when the second RegisterType was executed.
At this stage, I have added Container.Build after the first RegisterType and that was the source

procedure RegisterTypes(Container: TContainer);
begin
  Container.RegisterType<IMyFirstInterface, TMyFirstClass>(
    function: TMyFirstClass
    begin
      result := TMyFirstClass.Create(nil);
    end).AsSingleton(TRefCounting.True);

  Container.Build;
  Container.RegisterType<IMySecondIterface, TMySecondClass>('ABC').AsCustom(TMySecondClass.Create(nil, Container.Resolve
    <IMyFirstInterface>)).AsSingleton; //EIntfCastError Interface not supported

  ...
end;

this time I have a "EIntfCastError Interface not supported" error message when the application tries to execute the second RegisterType

The following is the Interface declaration:
  IMainDM = interface
    ['{CA5F2949-F634-40A6-8E82-222F52FB4B5D}']
    methods....
  end;


What else should I change to progress now?

Many thanks
Alberto

Stefan Glienke

unread,
Feb 15, 2024, 5:37:24 AMFeb 15
to Spring4D
The code you provide in the activator callback is completely pointless because that is what the container does.
Providing such a callback is only necessary if the container will not be able to construct the instance because it is fetched some something ouf of control for the container.

Pio Pio

unread,
Feb 18, 2024, 6:24:01 AMFeb 18
to Spring4D
Hello,

I have looked again into all the suggestions and realised what everybody was telling me. I have amended the registration of all the classes with the following syntax and it works now:

  IMyFirstInterface = interface
    ['{1179C0D0-F340-43C2-A004-970F466CA513}']
//methods
  end;
  IMySecondInterface = interface
    ['{654076DA-377D-45DF-9A5F-E571E58AE116}']
//methods
  end;

  TMyFirstClass = class(TInterfacedObject, IMyFirstInterface)
    // methods
  end;
  TMySecondClass = class(TInterfacedObject, IMySecondInterface)
    // methods
  end;

  procedure RegisterInContainer;
  begin
    GlobalContainer.RegisterType<TMyFirstClass>(
function: TMyFirstClass
    begin
      result := TMyFirstClass.Create;
    end).Implements<IMyFirstInterface>;

    GlobalContainer.RegisterType<TMySecondClass>(
    function: TMySecondClass
    begin
      result := TMySecondClass.Create(Container.Resolve<IMyFirstInterface>);
    end).Implements<IMySecondInterface>('ABC');

...
GlobalContainer.Build;
  end;



Thank you very much to everyone!

Stefan Glienke

unread,
Feb 19, 2024, 4:40:58 PMFeb 19
to spri...@googlegroups.com
The use of the activator callbacks is unnecessary. The container knows how to call constructors.

--
You received this message because you are subscribed to a topic in the Google Groups "Spring4D" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spring4d/qsZAAALc-wQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spring4d+u...@googlegroups.com.

Dr Kristau

unread,
Apr 2, 2024, 4:32:54 AMApr 2
to Spring4D
I'm still confused as to the syntax to replace .DelegateTo in the case of constructor injection, as in the example here.

What can I use to replace these calls?

  GlobalContainer.RegisterType<string>('name').DelegateTo(
                                   function: string
                                   begin
                                     Result := GetLocalUsername;
                                   end
                                   );
  GlobalContainer.RegisterType<string>('occupation').DelegateTo(
                                   function: string
                                   begin
                                     Result := 'plumber';
                                   end
                                   );

  GlobalContainer.RegisterType<integer>('age').DelegateTo(
                                   function: integer
                                   begin
                                     Result := Random(100);
                                   end
                                   );

Thank you!

Stefan Glienke

unread,
Apr 2, 2024, 11:20:05 AMApr 2
to Spring4D
You pass the delegate as the first parameter to RegisterType.
Reply all
Reply to author
Forward
0 new messages