Problem using RegisterType on GlobalContainer a TDataModule Class

Issue #59 resolved
Ezequiel Juliano Müller created an issue

I'm trying to register a Class of TDataModule type in container, but when I try to solve it is being raised the following exception:

"No component was registered for the service type: TDm."

From what I realized TDataModule inherits from TComponent that implements an interface IInterfaceComponentReference and when it is done the record this is causing a problem in the method:

procedure TInterfaceInspector.DoProcessModel(const context: IContainerContext;
  const model: TComponentModel);

If I comment out the line works:

...
if TType.IsDelegate(model.ComponentTypeInfo) then
      context.ComponentRegistry.RegisterService(model, model.ComponentType.Handle);

    //if model.Services.IsEmpty then Comment
      context.ComponentRegistry.RegisterService(model, model.ComponentType.Handle);
...

Code:

var
  vDm: TDm;
begin
  GlobalContainer.RegisterType<TDm>;
  GlobalContainer.Build;
  vDm := GlobalContainer.Resolve<TDm>;
end;

Comments (1)

  1. Stefan Glienke repo owner

    Thanks, this already has been fixed for the next version.

    Also there is another problem which would have bitten you after solving this one - the container does not know that it should call the TComponent.Create because it has nothing to inject for the AOwner argument and thus falls back to the TObject.Create which will leave you with a half initialized instance.

    I am looking into finding a good way to work with injecting nil in this case (because the container usually permits injecting nil).

    Until then to get it working with 1.1 you have to register like this:

    container.RegisterType<TDm,TDm>.DelegateTo(
      function: TDm
      begin
        Result := TDm.Create(nil);
      end);
    
  2. Log in to comment