Sprin4D, register factory

Sprin4D, register factory

I have a class that implements the interface:

TMyClass = class(TInterfacedObject, IMyInterface)
public
constructor Create(ParamA : string; ParamB : Integer; ParamC : Boolean);
end;

and the factory:

TMyInterfaceFactoryA = reference to function(ParamA : string; ParamB : Integer; ParamC : Boolean) : IMyInterface;

which I register in the spring:

Container.RegisterType;
Container.RegisterFactory;

Then I resolve the factory and create an interface, it works ok.

But I would like a second factory that will always give ParamC a constant value, e.g. True

I tried this:

TMyInterfaceFactoryB = reference to function(ParamA : string; ParamB : Integer) : IMyInterface;

but I get error when resolving:

'Unsatisfied constructor on type TMyClass'

How to declare and register such a factory?

Comments

  1. Stefan Glienke
    Of course you were right, everything works correctly. The problem for me was the incomplete registration and "losing" by the container of one of the constructors.
    Is it possible to force Spring to report an unknown, unregistered type used in the constructors?
    At the moment, the report is poor, with a complex hierarchy of classes and extensive constructors a lot of time is lost in searching for missing registrations :-(

    Eg.

    classes:

    TUnknownClass = class
    end;

    TExampleClass = class
    public
    constructor Create(aUnknown : TUnknownClass);
    end;

    {$M+}
    TExampleClassFactory = reference to function : TExampleClass;
    {$M-}

    registrations:

    aContainer.RegisterType;
    // >>>>>> missing TUnknownClass type registration
    aContainer.RegisterFactory;

    usage:
    var
    lExample: TExampleClass;
    begin
    lExample := aExampleClassFactory();

    and error:

    'Unsatisfied constructor on type: ss.Framework.Base.SafeQueryProvider.TExampleClass'

    There is only information WHERE is missing and not WHAT.

    Would it be possible to detect the lack of TUnknownClass registration during the TExampleClass class registration and add this fact in the message? I lost a few hours to look for such details, and if Spring were to report it would be wonderful :-)

    ReplyDelete
  2. "Would it be possible to detect the lack of TUnknownClass registration during the TExampleClass class registration and add this fact in the message?"

    No, because order of registration does not have to be like that, can easily register TUnknownClass after that or provide it by some other way. Proper ctor to be called are currently determined at resolve time.

    Even when calling container.Build it currently is not possible to determine that because type registrations are only processed after each other. For that to work at that point it would need to do a second run checking and selecting ctors after all types are known to the container. It even is something that I want for quite some time because that would improve resolve performance significantly because it can already build a plan of what to create without determining that over and over.

    What we are doing at work is putting some information out via the ILogger interface inside the type resolver so we can see in the log what ctors it inspects and which it picks (or does not).

    That being said I have been planning quite some changes for 1.3 that also touch on this topic because once your object graph gets huge using the container requires some way of diagnostic possibilities. But the curse of the current flexibility of the container makes this not so easy as there are so many corner cases and exceptional use cases that are all valid that I don't want to break (or at least be aware of to put them into some breaking change list and provide alternative ways to solve the task)

    ReplyDelete
  3. Stefan Glienke Ok, it does not have to be checked at the registration stage or even Build. It would be enough for the resolver, in the place where it analyzes the available constructors, to detect the type that is not registered, and then that this information would be passed higher and if an exception occurs, its message would contain information about this unknown type.
    In general, I would like to get info about the fact that the resolver does not know the TUnknownClass type when trying to create a TExampleClass object. That's all. I know it's not trivial :-)
    Thanks for you works, I waiting for S4D 1.3

    ReplyDelete

Post a Comment