WG: [spring4d] Registering anonymous methods directly to interfaces

158 views
Skip to first unread message

Glienke, Stefan

unread,
Jun 18, 2012, 4:59:21 AM6/18/12
to spri...@googlegroups.com
After taking a look at the implementation I am very sure that leaving out the implementing class will not work with the current architecture of the container.

But since you most likely do a create in the delegate you can specify that class at the RegisterType method as I did in the example.

If you are facing any problems please post some code to reproduce the problem and also the Delphi version you are using.

-----Ursprüngliche Nachricht-----
Von: Glienke, Stefan
Gesendet: Montag, 18. Juni 2012 08:19
An: 'spri...@googlegroups.com'
Betreff: AW: [spring4d] Registering anonymous methods directly to interfaces

I don't see any problem with your example code:

program Project1;

{$APPTYPE CONSOLE}

uses
Spring.Collections,
Spring.Collections.Lists,
Spring.Container,
SysUtils;

type
ISomeInterfaceItem = interface
end;

ISomeInterfaceList = interface(IList<ISomeInterfaceItem>)
['{6DDC6D91-F2AE-45B3-AE37-653722E48546}']
end;

TSomeInterfaceList = class(TList<ISomeInterfaceItem>, ISomeInterfaceList)
end;

begin
GlobalContainer.RegisterType<TSomeInterfaceList>.Implements<ISomeInterfaceList>.DelegateTo(
function: TSomeInterfaceList
begin
Result := TSomeInterfaceList.Create;
end);

GlobalContainer.Build;

with GlobalContainer.Resolve<ISomeInterfaceList> do
begin
Add(nil);
Writeln(Count);
end;

Readln;
end.

However I like the idea of registering interface directly.

-----Ursprüngliche Nachricht-----
Von: spri...@googlegroups.com [mailto:spri...@googlegroups.com] Im Auftrag von Atle Smelvær
Gesendet: Sonntag, 17. Juni 2012 16:33
An: Spring Framework for Delphi
Betreff: [spring4d] Registering anonymous methods directly to interfaces

There are some issues today with generic classes and interfaces, and
it's not possible to register them due to the GUID problem.

Take this scenario:

You have a
ISomeInterfaceList = interface(IList<ISomeInterfaceItem>)

If you try to make a
TSomeInterfaceList =
class(Spring.Collections.Lists.TList<ISomeInterfaceItem>,ISomeInterfaceList)
end

you will not be able to do this because some of the interface methods
are private on TList<T>.

However, if we had a way to register anonymous methods directly to
interfaces like this:

GlobalContainer.RegisterDelegate(
function: ISomeInterfaceList
begin
Result :=
ISomeInterfaceList(Spring.Collections.TCollections.CreateList<ISomeInterfaceItem>);
end
).Implements<ISomeInterfaceList>;

It could be possible to do different tricks to handle this. The clue
is in any way to provide small factory functions directly into the
registration without the need of a spesific class type.

Does this sound like something that could be interesting to add to the
framework?

--
You received this message because you are subscribed to the Google Groups "Spring Framework for Delphi" group.
To post to this group, send email to spri...@googlegroups.com.
To unsubscribe from this group, send email to spring4d+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spring4d?hl=en.


Atle Smelvær

unread,
Jun 19, 2012, 3:17:56 PM6/19/12
to Spring Framework for Delphi
The sample you provided works nice. But I ended up moving

function GetOnChanged: ICollectionChangedDelegate<T>;
function GetOnNotify: ICollectionNotifyDelegate<T>;

from private to protected in TListBase<T> inside the Spring framework.
I hope this get adjusted in the repository later on.

and then I did this

ISomeInterfaceList = interface(IList<ISomeInterfaceListItem>)
['{..}']
end;

and in implementation part

type
  TSomeInterfaceList =
class(Spring.Collections.Lists.TList<ISomeInterfaceListItem>,ISomeInterfaceList)
  end;

initialization
 
GlobalContainer.RegisterType<TSomeInterfaceList>.Implements<ISomeInterfaceList>;

Glienke, Stefan

unread,
Jun 20, 2012, 1:48:27 AM6/20/12
to spri...@googlegroups.com
I still don't see the point why the getters need to be protected.
Please tell me

-----Ursprüngliche Nachricht-----
Von: spri...@googlegroups.com [mailto:spri...@googlegroups.com] Im Auftrag von Atle Smelvær
Gesendet: Dienstag, 19. Juni 2012 21:18
An: Spring Framework for Delphi
Betreff: Re: WG: [spring4d] Registering anonymous methods directly to interfaces

Atle Smelvær

unread,
Jun 20, 2012, 7:32:36 AM6/20/12
to spri...@googlegroups.com
They are used by the IList<T> interface, and I cannot define a TSomeClass = class(..TList<Intf>,IList<Intf>) without this.

Glienke, Stefan

unread,
Jun 20, 2012, 7:44:38 AM6/20/12
to spri...@googlegroups.com

Sorry, but that is not correct. Since the events themselves are generic the T correctly gets specified there.

 

Example:

 

program Project11;

 

{$APPTYPE CONSOLE}

 

uses

  Spring.Collections,

  Spring.Collections.Lists,

  SysUtils;

 

type

  ISomeIntf = interface

  end;

 

  TSomeClass = class(TList<ISomeIntf>, IList<ISomeIntf>)

  public

    procedure DoOnChanged(Sender: TObject; const Item: ISomeIntf;

      Action: TCollectionChangedAction);

  end;

 

procedure TSomeClass.DoOnChanged(Sender: TObject; const Item: ISomeIntf;

  Action: TCollectionChangedAction);

begin

  Writeln('changed');

end;

 

var

  some: TSomeClass;

begin

  some := TSomeClass.Create;

  some.OnChanged.Add(some.DoOnChanged);

  some.Add(nil);

  Readln;

end.

 

Von: spri...@googlegroups.com [mailto:spri...@googlegroups.com] Im Auftrag von Atle Smelvær
Gesendet: Mittwoch, 20. Juni 2012 13:33
An: spri...@googlegroups.com
Betreff: Re: WG: [spring4d] Registering anonymous methods directly to interfaces

 

They are used by the IList<T> interface, and I cannot define a TSomeClass = class(..TList<Intf>,IList<Intf>) without this.

--

You received this message because you are subscribed to the Google Groups "Spring Framework for Delphi" group.

To view this discussion on the web visit https://groups.google.com/d/msg/spring4d/-/HqpIOX8zvEsJ.


To post to this group, send email to spri...@googlegroups.com.
To unsubscribe from this group, send email to spring4d+u...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/spring4d?hl=en-US.

Atle Smelvær

unread,
Jun 20, 2012, 8:37:17 AM6/20/12
to spri...@googlegroups.com
The compiler crashed with missing implementation here, that's why I moved it. Then it compiled. I found it odd that it did not work since IList<T> was already defined. Anyway, I updated to the latest and it just works now. Not sure what happened (maybe a bug in the compiler), but as long as it works properly, I'm happy.

-Atle

Glienke, Stefan

unread,
Jun 20, 2012, 10:20:12 AM6/20/12
to spri...@googlegroups.com

as long as it works properly, I'm happy”

 

Me too J

 

Von: spri...@googlegroups.com [mailto:spri...@googlegroups.com] Im Auftrag von Atle Smelvær
Gesendet: Mittwoch, 20. Juni 2012 14:37
An: spri...@googlegroups.com
Betreff: Re: WG: [spring4d] Registering anonymous methods directly to interfaces

 

The compiler crashed with missing implementation here, that's why I moved it. Then it compiled. I found it odd that it did not work since IList<T> was already defined. Anyway, I updated to the latest and it just works now. Not sure what happened (maybe a bug in the compiler), but as long as it works properly, I'm happy.

 

-Atle

--

You received this message because you are subscribed to the Google Groups "Spring Framework for Delphi" group.

To view this discussion on the web visit https://groups.google.com/d/msg/spring4d/-/I_V1PEDBmZEJ.

Baoquan Zuo

unread,
Jun 20, 2012, 10:21:03 AM6/20/12
to spri...@googlegroups.com
I must be in the list :)
Reply all
Reply to author
Forward
0 new messages