Error "Service not found @ServiceName" when default Implamentation for @ServiceType is exist

Issue #236 invalid
Inkvizi created an issue
  IOrder = Interface
    GetOrderType: string;
    ...
  end;
  TOrderA = class (TInterfacedObject, IOrder);
  TOrderB = class (TOrderA);
  TOrderC = class (TInterfacedObject, IOrder);

  IOrderProcessor = interface
    function ProcessOrder(Order: IOrder): ProcessResult;
    ...
  end;
  TOrderProcessorDefault = class (TInterfacedObject, IOrderProcessor)
  TOrderProcessorC = class (TInterfacedObject, IOrderProcessor)

TOrderProcessorDefault can process TOrderA and TOrderB And TOrderProcessorC can process TOrderC.

I have next refgistrations:

GlobalContainer.RegisterType<TOrderProcessorC>.Implements<IOrderProcessor>('OrderC').AsSingletonPerThread;
GlobalContainer.RegisterType<TOrderProcessorDefault, IOrderProcessor>.AsSingletonPerThread.AsDefault;

And I receive exception @SServiceNotFound for call

GlobalContainer.Resolve<IOrderProcessor>(Order.GetOrderType).ProcessOrder(Order);

when Order is not TOrderC. May by it will be better to call default implamentation of IOrderProcessor if found than raise exception? Or may be exist another solution for this case?

Comments (3)

  1. Stefan Glienke repo owner

    If you pass a name that it does not know it does not fall back to the default, as designed.

    If you need behavior like that write your own factory that does this selection mechanism.

  2. Log in to comment