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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:44:47+00:00 2026-05-20T02:44:47+00:00

I stumbled across some code and I was curious as to possible reasons why

  • 0

I stumbled across some code and I was curious as to possible reasons why it would be this way.

There are two constructors, the main one being called in code and the second one being used in testing for dependency injection. The second constructor accepted a function that returns the object rather than an instance of the object itself.

MainConstructor()  
:this(() => Factory.Current.GetInstance<IQueryService>()){ 
}

SecondConstructor(Func<IQueryService> getQueryService){
}

I was curious as to advantages you would get from passing in a function instead of an instance.

  • 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-20T02:44:48+00:00Added an answer on May 20, 2026 at 2:44 am

    There are several possible benefits for doing something like this:

    1. Decoupling: By allowing the caller to pass in a delegate rather than an instance, both the caller and the callee (in your case the class being constructed) can be decoupled from the object being passed and where it comes from.
    2. Lazy Acquisition: By passing in a delegate, the instance can be acquired in a lazy manner. In other words, the constructor could in principle cache the delegate and call it at the last possible moment, only when an instance is actually needed. If the instance is never needed, it may never get created.
    3. Recreatability: By passing in a delegate rather than an instance, the class can cache the mechanism used to create a query service on demand. In this way, the delegate acts as a factory, allowing instances of the type needed to be created on demand.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just stumbled across this bug in some legacy code: class MyAPIHandler { private:
I stumbled across this code and am too proud to go and ask the
I recently stumbled across this entry in the google testing blog about guidelines for
I've stumbled across this great post about validating parameters in C#, and now I
I stumbled across my rather ancient photo objects disks, and sadly found out the
I'm trying out ASP.NET MVC routing and have of course stumbled across a problem.
I am working on a project using Castle Active Record. I stumbled across the
I stumbled over this passage in the Django tutorial : Django models have a
Ever stumbled on a tutorial that you feel is of great value but not
Today I stumbled about a Problem which seems to be a bug in 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.