ImplementsAttribute?

85 views
Skip to first unread message

Nick Hodges

unread,
May 11, 2013, 3:54:11 PM5/11/13
to spri...@googlegroups.com
If I have the following class:

type
    [Implements(TypeInfo(IDoSomething))]
    TDemoClass = class(TInterfacedObject, IDoSomething)
      procedure DoSomething;
    end;

It appears that I still have to call:

GlobalContainer.RegisterType<TDemoClass>;

in order for things to all work.

Why is that?  Why can't the Implements attribute be enough?

Nick

Baoquan Zuo

unread,
May 12, 2013, 3:44:17 AM5/12/13
to spri...@googlegroups.com
ImplementsAttribute is useless unless you need to specify a name. Maybe it is also good to introduce ComponentAttribute and use RTTI to automatically register all components.

--
You received this message because you are subscribed to the Google Groups "Spring Framework for Delphi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spring4d+u...@googlegroups.com.
To post to this group, send email to spri...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nick Hodges

unread,
May 12, 2013, 7:32:49 AM5/12/13
to spri...@googlegroups.com
It still appears to fail even with a name (Sample below....)

I'd love to see it all registered via attributes.

unit uSpringAttributesDemo;

interface

uses
      Spring.Services
    , Spring.Container
    , SysUtils
    ;

type
  IDoSomething = interface
  ['{54FF5D46-4DBE-4AC9-9594-C36D1EEBDA74}']
    procedure DoSomething;
  end;

type
    [Implements(TypeInfo(IDoSomething), 'blah')]
    TDemoClass = class(TInterfacedObject, IDoSomething)
      procedure DoSomething;
    end;

    TDemoClassUser = class
      [Inject('blah')]
      FDoSomething: IDoSomething;
      procedure DoTheThing;
    end;

procedure DoIt;

implementation

procedure DoIt;
var
  DCU: TDemoClassUser;
begin
  DCU := TDemoCLassUser.Create;
  try
    DCU.DoTheThing;
  finally
    DCU.Free;
  end;
end;

{ TDemoClass }

procedure TDemoClass.DoSomething;
begin
  WriteLn('Doing something');
end;

{ TDemoClassUser }

procedure TDemoClassUser.DoTheThing;
begin
  FDoSomething.DoSomething;
end;

initialization
  GlobalContainer.RegisterType<TDemoClass>;
  GlobalContainer.RegisterType<TDemoClassUser>;
  GlobalContainer.Build;
end.

Nick Hodges

unread,
May 12, 2013, 11:03:24 AM5/12/13
to spri...@googlegroups.com
"Maybe it is also good to introduce ComponentAttribute and use RTTI to automatically register all components."

I'm going to work no this.  


On Sun, May 12, 2013 at 3:44 AM, Baoquan Zuo <baoqu...@gmail.com> wrote:

Glienke, Stefan

unread,
May 13, 2013, 1:59:19 AM5/13/13
to spri...@googlegroups.com

That will not work unless you cause the linker to not remove the class after compilation because it will not referenced anywhere.

 

When I need to do that I usually add a TMyClass.ClassName; call in the initialization part but you can easily forget that.

 

If you are looking for something like that check out https://code.google.com/p/delphisorcery/source/browse/trunk/Source/ComponentModel/DSharp.ComponentModel.Composition.SpringContainer.pas

Nick Hodges

unread,
May 13, 2013, 10:33:38 AM5/13/13
to spri...@googlegroups.com
Stefan --

Yeah, I eventually figured out that was what was happening.  ;-)

As always, I need to give Delphi Sorcery a look before I ask.  ;-)

Nick
Reply all
Reply to author
Forward
0 new messages