Unsatisfied constructor

58 views
Skip to first unread message

Lucas Thesenvitz

unread,
Sep 9, 2024, 7:09:38 AM9/9/24
to Spring4D
Hello,
I just started working with Spring4D and want to use a container to resolve a class MyClass that receives a TFDTable and a TFDQuery as parameters for the constructor. But I get the exception 'Unsatisfied constructor on type: MyClass' although I have registered TFDTable and TFDQuery in the container. Does anyone have an idea what the error could be?

Regards
Lucas
Message has been deleted

Adam Siwoń

unread,
Sep 10, 2024, 6:06:28 PM9/10/24
to Spring4D
I think ServiceLocator cannot find constructor without parameters which could be used to create instance of object so you must to pass correct parameter which will be used during constructor calling.  If you want to pass external param to the constructor you could use factory interface:

1. Create factory interface:
IFactory = interface(IInvokable)
  [GUID]
  function Produce(AOwner: TComponent): TFDTable;
end;
2. Register
GlobalContainer.RegisterType<TFDTable>.Implements<TFDTable>;
GlobalContainer.RegisterType<IFactory>.AsFactory;
3. Use it:
var table: TFDTable := ServiceLocator.GetService<IFactory>.Produce(TableOwner);

I hope it helps.

Regards
Adam

Stefan Glienke

unread,
Sep 10, 2024, 6:12:41 PM9/10/24
to Spring4D
I don't know about TFDTable and TFDQuery but usually TComponent descendants don't a specific owner in their ctor - passing nil is fine.
Spring has a special resolver for those TComponent descendants that is integrated: TComponentOwnerResolver in Spring.Container.Resolvers.pas

You don't need to do anything special it should just take care of passing nil to that virtual TComponent constructor that requires that AOwner: TComponent parameter.

If that does not work it requires you do provide a simple reproducible test case for me to look into it.

Reply all
Reply to author
Forward
0 new messages