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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:24:34+00:00 2026-06-18T00:24:34+00:00

I am trying to develop a library using dependency injection approach (with Ninject) and

  • 0

I am trying to develop a library using dependency injection approach (with Ninject) and I am having some kind of confusion likely because of my incorrect design. In summary, my design approach is

  1. A parent object has a common object.
  2. A parent object uses some variable number of child objects.
  3. All child objects should use the very same common object instance with their parent object

Here is a simple model of my problem domain.

interface IParent : IDisposable {
    void Operation();
}
interface ICommon : IDisposable {
    void DoCommonThing();
}
interface IChild1 {
    void DoSomething();
}
interface IChild2 {
    void DoAnotherThing();
}
class Parent : IParent {
    private readonly ICommon _common;
    public Parent(ICommon common) {
        _common = common;
    }
    public void Dispose() {
        _common.Dispose();
    }
    public void Operation() {
        var c1 = ObjectFactory.GetInstance<IChild1>();
        c1.DoSomething();
        var c2 = ObjectFactory.GetInstance<IChild2>();
        c2.DoAnotherThing();
        // number of childs vary, do things until cn
        _common.DoCommonThing();
    }
}
class Common : ICommon {
    private bool _isDisposed;
    public void Dispose() {
        _isDisposed = true;
    }
    public void DoCommonThing() {
        if (_isDisposed) 
            throw new Exception("Common Object is Disposed");
    }
}
class Child1 : IChild1
{
    private readonly ICommon _common;
    public Child1(ICommon common) {
        _common = common;
    }
    public void DoSomething() {
        // Do Something...
        _common.DoCommonThing();
    }
}
class Child2 : IChild2 {
    private readonly ICommon _common;
    public Child2(ICommon common) {
        _common = common;
    }
    public void DoAnotherThing() {
        // Do Another Thing...
        _common.DoCommonThing();
    }
}

Problem 1

Number of needed child objects vary. For example, according to return value of c1.DoSomething I may or may not need other child objects. So I do not want to inject them through constructor and just create them when they are needed. But this approach causes violation of Hollywood Principle.

Question 1

How this violation can be prevented, without injecting child objects through constructor?

Problem 2

I want child objects to use same common object instance with their parent object. So life time of common object should be same as its parent.

  1. If there is no life time is defined for ICommon then all child objects will have their own common object instance.

  2. If life time of ICommon is defined in Thread or Request scope then I cannot use different instances of parent object in same Thread or Request scope. Because each parent object should use their own brand new common object and dispose it.

So I could not solve it using life time scope options that I know. I produced another solution for this second problem but it makes code worse.

First, instead of injecting ICommon into parent object, parent object it self creates it through ObjectFactory

class Parent : IParent {
    private readonly ICommon _common;
    public Parent() {
        _common = ObjectFactory.GetInstance<ICommon>();
    }
.....

Then, instead of injecting ICommon into child object, parent object sets common object of child objects.

interface IChild {
    ICommon Common { get; set; }
}
interface IChildN : IChild {
     void DoNthThing();
}
abstract class ChildBase : IChild {
    ICommon IChild.Common { get; set; }
}
class ChildN : IChildN {
     public void DoNthThing() { }
}
class Parent : IParent {
    private readonly ICommon _common;
    public void Operation() {
        var c1 = ObjectFactory.GetInstance<IChild1>();
        c1.Common = _common;
        c1.DoSomething();
        var c2 = ObjectFactory.GetInstance<IChild2>();
        c2.Common = _common;
        c2.DoAnotherThing();
        _common.DoCommonThing();
    }
}

But this solution violates Hollywood Principle again and I have to set Common property of each child object.

Question 2

How can parent object distribute its common object to child objects using Dependency Injection? (preferably with Ninject)

Question 3

This is a little bit more general about my problem: How can Dependency Injection applied correctly to this model?

NOTE: ObjectFactory.GetInstance calls Kernel.Get of Ninject

  • 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-18T00:24:35+00:00Added an answer on June 18, 2026 at 12:24 am

    You need to use either the CallScope or the NamedScope. These are part of the Ninject.Extensions.NamedScope package. This allows you to scope the common object to the parent so all child requests receive the same common.

    Regarding the child object creation. If you have to request child objects depending on some algorithm you need to instantiate it with a factory. Use the Ninject.Extensions.Factory package to achieve this. This does a context preserving get and passes the parent context to the child request and therefore allows to reuse your common object also in the children which are created by the factory.

    So in the end there will be no need to use your own object factory.

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

Sidebar

Related Questions

I'm trying to develop a custom PDF viewer using PDFLibNet library. I downloaded compiled
I am trying to develop an android application by using some native libraries.However, the
I'm trying to develop an application using jeremy feinstein's SlidingMenu library which I found
I'm currently trying to develop an interactive chart using the RaphaelJS vector library. I
I am trying to develop a game and I was having issues managing the
I am trying to develop auto horizontal scrolling for our website using - jQuery.ScrollTo
I'm trying to develop an external library (not sure if that's the right term)
I am trying to develop an application that uses MySQL using C++. I downloaded
Im trying to develop an application in php to download pics from picasa using
I'm trying to develop a dynamic library in C++ to be called by an

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.