Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5977347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:21:27+00:00 2026-05-22T21:21:27+00:00

I am writing a simple dependency injection / inversion of control system based on

  • 0

I am writing a simple dependency injection / inversion of control system based on a TDictionary holding abstract class references with their respective implementor classes.

My goals are:

  • Avoid direct instantiation by type (obviously).
  • Inclusion of a class’ unit in the dpr should be enough to have it registered and be available for selection and instantiation through the di/ioc system.
  • Declare concrete implementing classes in implementation section only.
  • Use class constructors instead of initialization sections.

Btw, I am aware that using class constructors to take advantage of smart linking and wanting the inclusion of a unit to be enough to make a class available are defeating each other. I want to use class constructors instead of initialization sections for other reasons as well. And I would like to keep all class initialization/registration code together instead of having to split it between the class constructor and initialization section.

Problem

I want the registration of the class into the factory to be in the class constructor. Unfortunately, the compiler doesn’t think the class is “touched” by just using its type in its own class constructor.

When I put the registration function in the initialization section, then the compiler does think the class is touched and calls the class constructor. But that defeats the object of my exercise of keeping all class initialization code in the class constructor.

Two questions

  • Should the compiler consider the use of the class in its own class constructor “touching the class” or is that too much to expect the compiler to do?
  • Does anybody have any clever ideas on how I can still achieve my goals without using the initialization section?

Example

The abstract classes used in the application:

TSite = class abstract (TObject)
  function GetURL: string; virtual; abstract;
  property URL: string read GetURL;
end;

TSites = class (TList<TSite>);

TThisApplication = class abstract (TObject)
  function Sites: TSites; virtual; abstract;
end;

The concrete implementing class (declared in the implementation section!) for TThisApplication

  TThisApplicationConcrete = class(TThisApplication)
  class constructor ClassCreate;
  strict private
    FSites: TSites;
    function Sites: TSites; override;
  end;

class constructor TThisApplicationConcrete.ClassCreate;
begin
  RegisterImplementorClass(TThisApplication, TThisApplicationConcrete);
end;

function TThisApplicationConcrete.Sites: TSites;
var
  SiteList: TSites;
begin
  if not Assigned(FSites) then begin
    SiteList := TSites.Create;  // Change to use factory
    //RetrieveSites(SiteList);
    FSites := SiteList;
  end;

  Result := FSites;
end;

The function to get an instance of TThisApplication:

function ThisApplication: TThisApplication;
var
  ImplementorClass: TClass;
begin
  ImplementorClass := GetImplementorClass(TThisApplication);
  if Assigned(ImplementorClass) then begin
    Result := ImplementorClass.Create as TThisApplication;
  end else begin
    Result := nil;
  end;
end;

This is currently coded in a separate function, but it w/could be moved to the factory.

Full example code

If anybody would like to experiment, I have the full code of my test projects available at : http://www.bjsoftware.com/delphistuff/stackoverdlow/classconstructors.zip

Zip contents:

  • 4 projects all using the same source files, differing only in conditional defines (that’s why the dproj’s are also included)
  • 4 source files
  • groupproj and its dsk with all 4 projects
  • RunTestApps.cmd to run all 4 projects
  • Results.txt with the output of my run of the RunTestApps.cmd
  • WriteUp.txt with the text of this question

Please bear in mind that at all times you need to do a “Build All Projecs” because all dcu’s and exe’s are going to the source dir and otherwise you are going to face a lot of errors and/or confusion because the exe isn’t doing what its name indicates.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:21:28+00:00Added an answer on May 22, 2026 at 9:21 pm

    This is as expected. As Uwe pointed out, a self-referential class constructor isn’t enough to trigger inclusion. Placing the reference in the initialization section will do the trick since that is outside the class itself. Trying to self-reference a class for inclusion is akin to trying pull yourself out of a deep hole by pulling on your own suspenders.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently writing a simple, timer-based mini app in C# that performs an
I'm learning Rails by writing simple TODO tasks aplication. Two models are: class List
I've been reading about dependency injection and have a simple question. I can understand
I'm writing simple program to communicate between smart devices and I receive 11001 when
I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
I am writing simple site that requires users and profiles to be handled. The
How long does it take for an experienced Windows programmer to learn writing simple
I'm writing a simple OpenGL application that uses GLUT . I don't want to
I'm writing a simple app that's going to have a tiny form sitting in
I am writing a simple checkers game in Java. When I mouse over the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.