Jump to content

Jacek Laskowski

Members
  • Content Count

    267
  • Joined

  • Last visited

  • Days Won

    2

Posts posted by Jacek Laskowski


  1. 15 hours ago, Stefan Glienke said:

    I meant the implementing class of the comparer or are you using the default from Generics.Defaults?

     

    Then just RegisterInstance(TEqualityComparer<whatever>.Default());

     

    Or make an overload without parameter where you create the default comparer and mark that one with [Inject] for the container to use that one because imo a comparer if it does not have dependencies itself falls into the category of a createable thus does not need to be injected.

     

    Ok, I paste more code with real example:

     

    program s4dgic;
    
    uses
      System.Generics.Defaults,
      Spring.Collections,
      Spring.Container,
      System.SysUtils;
    
    {$APPTYPE CONSOLE}
    {$R *.res}
    
    type
      TBaseClass<T, V> = class(TInterfacedObject)
      private
        fDict: IDictionary<T, V>;
      public
        constructor Create(const aComparer: IEqualityComparer<T>); virtual;
      end;
    
    constructor TBaseClass<T, V>.Create(const aComparer: IEqualityComparer<T>);
    begin
      inherited Create;
      fDict := TCollections.CreateDictionary<T, V>(aComparer);
    end;
    
    type
      RKey = record
        FieldA: Integer;
        FieldB: String;
      end;
    
      TMyEqualityComparer = class(TEqualityComparer<RKey>)
      public
        function Equals(const Left, Right: RKey): Boolean; override;
      end;
    
    function TMyEqualityComparer.Equals(const Left, Right: RKey): Boolean;
    begin
      Result := (Left.FieldA = Right.FieldA) and (Left.FieldB = Right.FieldB);
    end;
    
    type
      IMyIntf = interface ['{874B15D9-675E-428B-906A-D526FCC3AE0D}']
      end;
    
      TMainClass = class(TBaseClass<RKey, TObject>, IMyIntf)
      public
        constructor Create(const aComparer: IEqualityComparer<RKey>); override;
      end;
    
    constructor TMainClass.Create(const aComparer: IEqualityComparer<RKey>);
    begin
      inherited Create(aComparer);
    end;
    
    var
      C: TContainer;
      I : IMyIntf;
      
    begin
      try
        C := TContainer.Create;
    
        >>>>>  how to register required types?   <<<<
    
        C.RegisterType<TMainClass>.Implements<IMyIntf>;
        C.Build;
        I := C.Resolve<IMyIntf>;
        C.Free;
        Readln;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    
    end.

     


  2. 2 hours ago, Stefan Glienke said:

    Does the implementing class itself has dependencies the container should build? 

     

    Yes, I have class with IEqualityComparer<T> in constructor, so I need the container to be able to resolve this interface... but this is impossible?


     

    TMyClass<T> = class(TInterfacedObject, IMyInterface)
      constructor Create(const aKeyComparer : IEqualityComparer<T>);
    end;

     

     


  3. The class design is rather good, the override detection I need in order to optimize performance (class implements cache in the REST server).
    The ancestor (in base class) method returns always False, the descendant method can change this behavior, but if it does not change, I prefer to check the boolean flag rather than call the method (just because it's faster).


  4. Such a simple example with inheritance:

     

    TAncestor = class
      procedure MethodX(); virtual
      procedure Do();
    end;
    
    TDescendantA = class(TAncestor) //override MethodX
      procedure MethodX(); override;
    end;
    
    TDescendantB = class(TAncestor) //no override MethodX
    end;
    
    procedure TAncestor.Do();
    begin
     how to check if MethodX is overridden?  <<<<<<<<<<<<<<<<<<<<
    end;
    
    var
     A : TDescendatdA;
     B : TDescendantB;
    begin
     A := TDescendatdA.Create;
     B := TDescendatdB.Create;
     A.Do();
     B.Do();

     

    How to check if MethodX is overridden?

     


  5. 23 hours ago, Primož Gabrijelčič said:

    from Microsft site:

     

    " [Network DDE is no longer supported. Nddeapi.dll is present on Windows Vista, but all function calls return NDDE_NOT_IMPLEMENTED.] "

    • Thanks 1

  6. Yes, quality lies and cries. Recently, I have been working a little in VSCode and the node.js ecosystem. Environmental quality is ahead of Delphi for ages. In addition, it works steadily and quickly, although behind the scenes everything is based on slow JS!

    I have serious doubts that Delphi will ever catch up with the world in this area.


  7. I register classes and factories in Spring:

     

    container.RegisterType<TMyClass>;
    container.RegisterFactory<TMyClassFactory>;


    Then in the code I use:
     

     fClassFactory: TMyClassFactory;
    
    [...]  
      
    begin
      lMyClass: = fClassFactory();
      [some code]
      lMyClass.Free; <- who is responsible?
    end;



    Who is responsible for the release of the object? Spring or me?

     

×