TList and TDictionary descendant

Issue #352 wontfix
Alberto Zen created an issue

Good afternoon

I did same trials with TList and TDictionary descendants, If i create the instances for the objects using Create constructor everything works fine, But if i try to register the two types into GlobalContainer and then to obtain the instances via GetService function, the instances of the objects don’t work. If i try to add some item to the collections i receive an exception and the same when i close the application, when the destructor of the descendants are called.

I tried to put some breakpoints on the Create constructor code but i can’t see the program execution passing the breakpoints. The objects was instantiated into the correct type but it’s like the creation process doesn’t work fine. I attach a project to explain better the beaviour i met.

Best regards

Comments (1)

  1. Stefan Glienke repo owner

    The behavior you observe might very well be true because the parameterless ctor is not the one to be called to have a fully operational instance of some of the collection types hence the methods from TCollections - collections should always be created via them - all the implementing classes are implementation details that are also always subject to change - as happening in 2.0.

    I also advice against treating collection types as injectables but create them yourself using the aforementioned methods from TCollections. There is a chapter about the difference between creatables and injectables in Nicks book and also in this blog post: https://medium.com/better-programming/five-principles-of-dependency-injection-5bd0cca9cb04

    P.S.: FWIW due to the changes on the implementing classes ctors that are not virtual anymore in 2.0 the example code provided works with a slight change and that is calling inherited Create(0, nil, nil,[]); in TMyInterfaceDictionary.Create.

  2. Log in to comment