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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:13:47+00:00 2026-05-30T07:13:47+00:00

If I have a class, which has dual dependencies on the same type (needs

  • 0

If I have a class, which has dual dependencies on the same type (needs two different instances), if the only difference between the instances is a deeper dependency, what’s the best way to have Ninject perform DI and keep the two graphs separate?

Example object graph:

foo → ClassA → ClassB
bar → ClassA → ClassB

Class C‘s constructor:

public class C
{
    public C(ClassB foo, ClassB bar) { … }
}

So how do I make sure that the ClassB instantiated with foo gets supplied as the ClassB dependancy foo, and bar… bar?

Just for some background, I had some requirements change, so I need to replace a write-only repository (IAdd) with a composite write-only repository

public class CompositeWriteRepository<T> : IAdd<T>
{
    public CompositeWriteRepository(IAdd<T> foo, IAdd<T> bar, Func<T, bool> descriminator) { ... }
    public Add(T entity)
    {
        if (descriminator(entity)) {
            foo.Add(entity);
        } else {
            bar.Add(entity);
        }
    }        
}

With mocking, that was easy enough, I could just inject using names:

 kernel.Bind<IAdd<EntityType>>().To<fooRepository>().Named("foo");
 kernel.Bind<IAdd<EntityType>>().To<barRepository>().Named("bar");

 kernel.Bind<IAdd<EntityType>>().To<CompositeWriterRepository<EntityType>>()
    .WithConstructorArgument("foo", x => x.Kernel.Get<IAdd<EntityType>>("foo")
    .WithConstructorArgument("bar", x => x.Kernel.Get<IAdd<EntityType>>("bar");

The problem comes when I use the real repositories; foo and bar ultimately write to files so they need different file names. Since they’re StreamWriter repositories, one of their dependencies actually gets the two different file names.

string FileName → FileStreamWriterFactory → StreamRepository → CompositeRepository

The only way I’ve found thus far to construct things is to make a named FileName, named FileStreamWriterFactory, named StreamRepository × 2 (once for foo and once for bar). This seems like a lot of work, so I hope there’s a better solution.

I can re-architect if needed, it felt like an elegant way to quickly add in seperating the entries when the requirements changed. I realize my classes all are quite specific, but I think single responsibility would support this generally and in my case we write a bunch of small applications and have more requests the we can fulfill so its a good way to have lot’s of re-usable code laying around that I basically just need to re-configure for different tasks.

Solution
Remo Gloor should get credit; his is probably the best practice.

What I actually did was create a new extension

public static bool WhenAnchester(this IRequest request, Func<IRequest, bool> conditions)
{
    var parentContext = request.ParentContext;
    if (parentContext == null) {
            return false;
    }

    return conditions(parentContext.Request) || 
        parentContext.Request.WhenAnchester(conditions);
} 

This then let me easily control which file get injected into which repository.

kernel.Bind<string>().ToConstant("Foo.txt")
    .When(x => x.Target.Name == "filepath" && 
              x.WhenAnchester(t => t.Target != null && t.Target.Name == "Dest1"));

kernel.Bind<string>().ToConstant("Bar.txt")
    .When(x => x.Target.Name == "filepath" &&
               x.WhenAnchester(t => t.Target != null && t.Target.Name == "Dest2"));

There’s probably a better solution, so I wouldn’t necessary recommend this for others, but its working well for me.

  • 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-30T07:13:49+00:00Added an answer on May 30, 2026 at 7:13 am

    Yes there is a better way

    See https://github.com/ninject/ninject/commit/60443badf4ef840531c93e9287b154a9bba337c2

    It’s 3.0 but IsAnyAnchestorNamed can also be used with 2.2 from a When condition.

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

Sidebar

Related Questions

I have a class which has two HashSet<String> collections as private members. Other classes
Suppose I have some class which has a property actor_ of type Actor .
I have a class which has some properties of type List<float> , List<int> etc.
I have class which has a method that needs to return three DataTables. I
I have java class which has a variable of type char now I have
I have a class which has a property of type System.IO.FileAttribute (enum) Upon serializing
I have a class which has two attributes : set< int > ens1_; set<
I have a class which has two overloaded functions. How do I export it
I have a base class which has two child classes derived from it. class
I have User class which has a field type , which is in turn

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.