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

  • Home
  • SEARCH
  • 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 7620827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:02:22+00:00 2026-05-31T04:02:22+00:00

So, lets assume I can draw in paint: Say I have a class A

  • 0

So, lets assume I can draw in paint:

Horrible depiction of my problem

Say I have a class A which depends on objects B and C to be instanced, but C also depends on an instance of B, and I want this instance of B to be the same that I pass to A. How can I accomplish this?

Now, you probably didn’t understand that at all either; so I’ll go ahead and turn it into code:

public class A
{
    private readonly B b;
    private readonly C c;

    public A(B b, C c)
    {
        this.b = b;
        this.c = c;
    }
}

public class B
{    
}

public class C
{
    private readonly B b;

    public C(B b)
    {
        this.b = b;
    }
}

Without DI, I would resolve it like this:

var b = new B();
var c = new C(b);
var a = new A(b,c);

How can I accomplish something like this through DI, cleanly?
What I want is pretty straightforward: use the same instance of B when instancing both C and A.

Forgot to mention I do want this in a per-web-request lifestyle, not singleton or transient.

  • 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-31T04:02:24+00:00Added an answer on May 31, 2026 at 4:02 am

    According to the castle documentation, singleton behavior is already the default. Therefore, castle will create only one instance of B and pass it to both A and C.

    It’s the cases where you don’t want this that you should worry about. You need some extra configuration then, as described in the linked documentation.

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

Sidebar

Related Questions

Lets assume I want to save the count of datagrid rows which can be
This should be simple, but I can't figure it out For example, lets assume
Lets say I have a WCF service that a client can use to receive
Lets assume I have a list of objects in an array and i want
Lets assume we have a class in python: class A(object): def __del__(self): print Del!
Lets assume I have my browser load an Iframe with <iframe src=test.html> Can I,
Lets assume we have 2 instances of SQL Server. In the first instance I
Lets assume: List<element> which element is: public class Element { int Weight { get;
Let's assume I have the string NSString* myString = @Hello,; How can I remove
Lets assume we have this xml: <?xml version=1.0 encoding=UTF-8?> <tns:RegistryResponse status=urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Failure xmlns:tns=urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0 xmlns:rim=urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0> <tns:RegistryErrorList

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.