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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:19:49+00:00 2026-06-07T06:19:49+00:00

Here is the sequence in which objects need to be created. I’d like to

  • 0

Here is the sequence in which objects need to be created. I’d like to initialize the whole thing in SM and only use the last one as target:

var firstObject = new FirstClass("First", "Last");
var servers = new List<FirstClass> { firstObject };
var secondObject = new SecondClass();
secondObject.Servers = servers;
var thirdObject = new ThirdClass(secondObject);
var fourthObject = thirdObject.SomeGetFunction(thirdObject);

After all this is configured in SM, I’d like to basically only use

var objectToUse = ObjectFactory.GetInstance<fourthObject>();

So far I’m only able to get half way there, but can not figure out how complete this whole configuration. First three objects are singletons, they are only needed to properly instantiate and configure the fourth one.

  • 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-07T06:19:52+00:00Added an answer on June 7, 2026 at 6:19 am

    Here is how this can be accomplished. I’m not sure if this follows the best practices but gets the job done.

    The first object to be created needs some settings from the config file; this was easy.

    For<FirstClass>()
         .Singleton()
         .Use<FirstClass>()
         .Ctor<string>("First").EqualToAppSetting("FirstKey")
         .Ctor<string>("Last").EqualToAppSetting("LastKey");
    

    The second one was more problematic because after creation two properties needed to be set and one of them was of type IEnumerable<FirstClass>, which I couldn’t figure out how set up. As it turns out SM’s ‘context’ object and interception was the answer.

    For<SecondClass>()
         .Singleton()
         .Use<SecondClass>()
         .OnCreationForAll((context,x) => 
                               {
                                   var server = new FirstClass[1];
                                   server[0] = context.GetInstance<FirstClass>();
                                   x.Servers = server;
                                   x.Connections = 1;
                               });
    

    Third one was again easy, we can let SM figure out dependencies.

    For<ThirdClass>().Singleton().Use<ThirdClass>();
    

    And the fourth and final one was tricky again since the object I was after was supposed to be created by a call to the ThirdClass. Again, build in ‘context’ proved to be useful here.

    For<FourthClass>()
          .Singleton()
          .Use(context => context.GetInstance<ThirdClass>().SomeGetFunction("stringParam"));
    

    Now all I have to do is to call ObjectFactory.GetInstance<FourthClass> and do operations from there.

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

Sidebar

Related Questions

Here's one thing I haven't seen explicitly addressed in C++ expression template programming in
I have a large file which contains sequence of characters like ABCDEabcde.....XYZxyz. Now, I
Here is a typical sequence of events that occur : Host device opens a
Here is my code for finding a sequence in a string and replacing it
I have created a C# .net 2.0 webservice. I need to capture the raw
Im working on a java project where i need to read some objects from
I'm trying to get a TextBox control from a collection which only contains TextBoxes
So I'm trying to run some SQL here through PHP's PDO (which I don't
I have a sequence of ActiveRecord objects that I want to cascade destroy but
I've built a little camera capture daemon which captures a sequence of images from

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.