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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:20:07+00:00 2026-06-16T16:20:07+00:00

Let’s say I have the following classes that I want to construct using Ninject,

  • 0

Let’s say I have the following classes that I want to construct using Ninject, with the arrows showing dependencies.

A > B > D
A > C > D

I want to configure Ninject such that A is transient scoped, i.e. every time you ask Ninject for an A, you get a new one. I also want B and C to be transient, you get a new one of those every time you ask for an A. But I want the D to be reused across both B and C. So every time I request an A, I want Ninject to construct one of each object, not two Ds. But I don’t want Ds to be reused across different As.

What is the best way to set this up using Ninject?

Update:
After some more research, it seems like Unity has a PerResolveLifetimeManager which does what I’m looking for. Is there a Ninject equivalent?

  • 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-16T16:20:08+00:00Added an answer on June 16, 2026 at 4:20 pm

    Ninject supports four built in object scopes out of the box: Transient, Singleton, Thread, Request.

    So there isn’t any PerResolveLifetimeManager like scope but you can implement it easily with registering a custom scope with the InScope method.

    As it turned out there is an existing Ninject extension: ninject.extensions.namedscope which provides the InCallScope method which is what you are looking for.

    However if you want to do it yourself you can do with a custom InScope delegate. Where you can use the main IRequest object for the type A to use it as the scope object:

    var kernel = new StandardKernel();
    kernel.Bind<A>().ToSelf().InTransientScope();
    kernel.Bind<B>().ToSelf().InTransientScope();
    kernel.Bind<C>().ToSelf().InTransientScope();
    kernel.Bind<D>().ToSelf().InScope(
        c =>
            {
                //use the Request for A as the scope object                         
                var requestForA = c.Request;
                while (requestForA != null && requestForA.Service != typeof (A))
                {
                    requestForA = requestForA.ParentRequest;
                }
                return requestForA;
            });
    
    var a1 = kernel.Get<A>();    
    Assert.AreSame(a1.b.d, a1.c.d);
    
    var a2 = kernel.Get<A>();    
    Assert.AreSame(a2.b.d, a2.c.d);
    
    Assert.AreNotSame(a1.c.d, a2.c.d);
    

    Where the sample classes are:

    public class A
    {
        public readonly B b;
        public readonly C c;
        public A(B b, C c) { this.b = b; this.c = c; }
    }
    
    public class B
    {
        public readonly D d;
        public B(D d) { this.d = d; }
    }
    
    public class C
    {
        public readonly D d;  
        public C(D d) { this.d = d; }
    }
    
    public class D { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say I have an Instant Messenger server using SignalR. I want to broadcast
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords
Let's say that each Product has a category. I want to ask the Users
Let's say I have a file foo.py, and within the file I want to
let's say.. I have the following java bean. Case1: (Student Bean) Integer id; String

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.