Home > Net >  Spring4D - When registering a type with a container is it possible to specify only the arguments you
Spring4D - When registering a type with a container is it possible to specify only the arguments you

Time:05-12

Given a type and registration of

      TTest = class
      public
        constructor Create(First, Second: IO; Other: TOther);
      end;

  GlobalContainer.RegisterType<TOther,TOther>;
  GlobalContainer.RegisterType<IO,TO1>.AsDefault;
  GlobalContainer.RegisterType<IO,TO2>('Second');

As I want to override the second parameter I have to

  GlobalContainer.RegisterType<TTest>.InjectConstructor([nil,'Second',nil]);

I want to only override the second argument. I understand for argument placement I would also have to specify the first argument but as I add further and further constructor arguments I have to keep remembering to go back and modify the registration.

It would be easier to

GlobalContainer.RegisterType<TTest>.InjectConstructor([nil,'Second']);

rather than

GlobalContainer.RegisterType<TTest>.InjectConstructor([nil,'Second',nil,nil,nil,nil]);

as I add further dependencies as surely nil could be implied.

CodePudding user response:

This is currently not possible via the registration API but I have this on my list of things I want to implement at some point. My idea is to do it very similar to how autofac does it - no eta though I am afraid.

However, you can place an [Inject('second')] on the Second parameter of the TTest constructor to achieve this (remember to put Spring.Container.Common into the uses to make the attribute work).

  • Related