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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:09:41+00:00 2026-05-15T08:09:41+00:00

We have a concrete singleton service which implements Ninject.IInitializable and 2 interfaces. Problem is

  • 0

We have a concrete singleton service which implements Ninject.IInitializable and 2 interfaces. Problem is that services Initialize-methdod is called 2 times, when only one is desired. We are using .NET 3.5 and Ninject 2.0.0.0.

Is there a pattern in Ninject prevent this from happening. Neither of the interfaces implement Ninject.IInitializable. the service class is:

public class ConcreteService : IService1, IService2, Ninject.IInitializable
{
    public void Initialize()
    {
        // This is called twice!
    }
}

And module looks like this:

public class ServiceModule : NinjectModule
{
    public override void Load()
    {
        this.Singleton<Iservice1, Iservice2, ConcreteService>();
    }
}

where Singleton is an extension method defined like this:

    public static void Singleton<K, T>(this NinjectModule module) where T : K
    {
        module.Bind<K>().To<T>().InSingletonScope();
    }

    public static void Singleton<K, L, T>(this NinjectModule module) 
        where T : K, L
    {
        Singleton<K, T>(module);
        module.Bind<L>().ToMethod(n => n.Kernel.Get<T>());
    }

Of course we could add bool initialized-member to ConcreteService and initialize only when it is false, but it seems quite a bit of a hack. And it would require repeating the same logic in every service that implements two or more interfaces.


Thanks for all the answers! I learned something from all of them! (I am having a hard time to decide which one mark correct).

We ended up creating IActivable interface and extending ninject kernel (it also removed nicely code level dependencies to ninject, allthough attributes still remain).

  • 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-15T08:09:41+00:00Added an answer on May 15, 2026 at 8:09 am

    Ninject 3

    Ninject 3.0 now supports multiple generic types in the call to bind, what you are trying to do can be easily accomplished in a single chained statement.

    kernel.Bind<IService1, IService2>()
          .To<ConcreteService>()
          .InSingletonScope();
    

    Ninject 2

    You are setting up two different bindings K=>T and L=>T. Requesting instances of L will return transient instances of T. Requesting K will return a singleton instance of T.

    In Ninject 2.0, an objects scope is per service interface bound to a scope callback.

    When you have

    Bind<IFoo>...InSingletonScope();
    Bind<IBar>...InSingletonScope();
    

    you are creating two different scopes.

    You are saying
    “Binding to IFoo will resolve to the same object that was returned
    when .Get was called.”
    and
    “Binding to IBar will resolve to the same object that was returned
    when .Get was called.”

    you can chain the bindings together, but you will need to remove IInitializable as it will cause duplicate initialization when the instance is activated:

    kernel.Bind<IBoo>()
          .To<Foo>()
          .InSingletonScope();
          .OnActivation(instance=>instance.Initialize());
    
    kernel.Bind<IBaz>()
          .ToMethod( ctx => (IBaz) ctx.Kernel.Get<IBoo>() );
    

    or

    kernel.Bind<Foo>().ToSelf().InSingletonScope()
        .OnActivation(instance=>instance.Initialize());
    kernel.Bind<IBaz>().ToMethod( ctx => ctx.Kernel.Get<Foo>() );
    kernel.Bind<IBoo>().ToMethod( ctx => ctx.Kernel.Get<Foo>() );
    

    in order to get multiple interfaces to resolve to the same singleton instance. When I see situations like this, I always have to ask, is your object doing too much if you have a singleton with two responsibilities?

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

Sidebar

Ask A Question

Stats

  • Questions 531k
  • Answers 531k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try the following: Bind<IMongoRepository>().To<MongoRepository>().WhenInjectedInto<ArticleController>().WithConstructorArgument("topic", "Article"); Bind<IMongoRepository>().To<MongoRepository>().WhenInjectedInto<PagesController>().WithConstructorArgument("topic", "Pages"); Assuming that the… May 16, 2026 at 11:57 pm
  • Editorial Team
    Editorial Team added an answer something like ExtJs?. Here are some demos Desktop like webpage… May 16, 2026 at 11:57 pm
  • Editorial Team
    Editorial Team added an answer foreach($listNumberList->Listnumber as $value) echo $value; try this one May 16, 2026 at 11:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Say I have a singleton-ish, factory-ish, reflection-ish class that receives some input, and spits
I have a concrete class of logger that Microsoft.Unity will resolve ILogger to. The
I'd like to configure the Windsor container so that a single, singleton-style instance can
I have an apache webserver that is used to serve php and static web
The concrete example being I have lots of specfiles with Source0 : or other
I have an abstract class with a single concrete method. In this method I
I have an abstract Content entity in my EF4 model with a concrete subclass,
I'm trying to allow my business logic components to query for services when they
I have a little experience with WCF and would like to get your opinion/suggestion
Possible Duplicate: StructureMap singleton usage (A class implementing two interface) I'm currently designing a

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.