Register a function by attribute

38 views
Skip to first unread message

Anders Gustavsson

unread,
Mar 19, 2025, 5:53:12 PMMar 19
to Spring4D
With the declaration

var FFactory : TFactory<string,  TFactoryMethod<ITableQueryObject>>;
type TabellNamn = class(TCustomAttribute);


I do this to register the functions that have the attribute in my factory. But the registerFactoryMethod of course need another second parameter. But I cannot find out what?
 
procedure TSqlFunctions.RegisterFunctions;
var
  ctx       : TRttiContext;
  typ      : TRttiType;
  attr      : TCustomAttribute;
  method    : TRttiMethod;
  tblName   : string;
begin
  ctx := TRttiContext.Create;
  try
    typ := ctx.GetType(TSqlFunctions);
    for method in info.GetMethods do
    begin
      if (method.MethodKind <> mkFunction) or (method.Parent <> info) then
        Continue;
      for attr in method.GetAttributes do
        if attr is TabellNamn then
        begin
          tblName    := AnsiUpperCase(TabellNamn(attr).GetTabellName);
          if not FFactory.IsRegistered(tblName) then
            FFactory.RegisterFactoryMethod(
              tblName, method);
        end;
    end;
  finally
    ctx.Free;
  end;
end;

Stefan Glienke

unread,
Mar 19, 2025, 6:01:56 PMMar 19
to Spring4D
Your declaration of FFactory should be TFactory<string, ITableQueryObject>. Then you pass a function: ITableQueryObject as second parameter.

Anders Gustavsson

unread,
Mar 20, 2025, 5:14:55 AMMar 20
to Spring4D
I did some stupid trial here, but the problem is I don't know how to store
          if not FFactory.IsRegistered(tblName) then
          begin
            var x := method;
            FFactory.RegisterFactoryMethod(
              tblName,
              function: ITableQueryObject
              begin
                Result := ITableQueryObject(x.CodeAddress);
              end);
          end;

Compiles, but of course doesn't work!

Anders Gustavsson

unread,
Mar 20, 2025, 5:15:06 AMMar 20
to Spring4D
That's reasonable. But the function I should pass is identified by the "method" variable, but just putting "method" there does not work. That is where I am stuck.


onsdag 19 mars 2025 kl. 23:01:56 UTC+1 skrev sgli...@dsharp.org:

Anders Gustavsson

unread,
Mar 20, 2025, 6:47:07 AMMar 20
to Spring4D
Ah, think I got it! I am thinking "factory" but that is the wrong class in this case. A dictionary where I just store the class of the wanted object works much better (and easier!).
Reply all
Reply to author
Forward
0 new messages