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 7989617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:48:13+00:00 2026-06-04T12:48:13+00:00

It’s possible to register a class with a parameter expected to be passed from

  • 0

It’s possible to register a class with a parameter expected to be passed from the point of creation?

I know it can be done something like this:

GlobalContainer.RegisterType<TUserProcessor>.Implements<IUserUpgrader>.
AsTransient.DelegateTo(
    function: TUserProcessor
    begin
      Result := TUserProcessor.Create(GetCurrentUser);
    end
  );

But there the parameters are binded to the execution context where the container gets registered and not where the object get’s intantiated.

Something like this it’s possible for example?

GlobalContainer.Resolve<IMathService>([FCurrentUser]);

I know some peoble advocate to have very simple constructors, but there are times when a constructor parameter looks clearly the way to go:

  1. The object constructed needs the object parameter to work, so the reference must be satisfied. The parameter also makes that constraint much more obvious looking at the class.

  2. You can assign the reference in a method or property and raise and exception in every other method if you try to use the object without first making the assignment.. I don’t like writing this type of code it’s simply a waste of time, just use the constructor parameter and check there. Less code, the better IMO.

  3. Also the object being passed it’s local to the object that constructs the new object using the container (for example a Transaction object) and has some state (it’s not a new object that I can get with the container).

  • 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-06-04T12:48:15+00:00Added an answer on June 4, 2026 at 12:48 pm

    I have added resolver overrides as in Unity.

    So you can write:

    program Demo;
    
    {$APPTYPE CONSOLE}
    
    uses
      Classes,
      Spring.Container,
      Spring.Container.Resolvers;
    
    type
      IUserUpgrader = interface
        ['{ADC36759-6E40-417D-B6F7-5DCADF8B9C07}']
      end;
    
      TUser = class(TObject);
    
      TUserProcessor = class(TInterfacedObject, IUserUpgrader)
      public
        constructor Create(AUser: TUser);
      end;
    
    constructor TUserProcessor.Create(AUser: TUser);
    begin
      Writeln('called constructor with passed user');
    end;
    
    begin
      GlobalContainer.RegisterType<TUserProcessor>.Implements<IUserUpgrader>;
      GlobalContainer.Build;
    
      GlobalContainer.Resolve<IUserUpgrader>(
        TOrderedParametersOverride.Create([TUser.Create]));
    
      GlobalContainer.Resolve<IUserUpgrader>(
        TParameterOverride.Create('AUser', TUser.Create));
    
      Readln;
    end.
    

    Edit 17.09.2018:

    This changed quite some while ago and Resolve is accepting parameters via array of TValue. You can either pass values directly there which then have to match exactly the parameter list in the ctor. For only partially filling the parameters you can use TNamedValue or TTypedValue from Spring.pas

    Anyway I suggest using factories to be injected and consumed by your code and not reaching into the container (global one or not) Resolve method.

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

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.