Ninject: Singleton binding syntax?
        Posted  
        
            by Rosarch
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Rosarch
        
        
        
        Published on 2010-04-05T22:19:57Z
        Indexed on 
            2010/04/05
            22:23 UTC
        
        
        Read the original article
        Hit count: 190
        
I'm using Ninject 2.0 for the .Net 3.5 framework. I'm having difficulty with singleton binding.
I have a class UserInputReader which implements IInputReader. I only want one instance of this class to ever be created.
 public class MasterEngineModule : NinjectModule
    {
        public override void Load()
        {
            // using this line and not the other two makes it work
            //Bind<IInputReader>().ToMethod(context => new UserInputReader(Constants.DEFAULT_KEY_MAPPING));
            Bind<IInputReader>().To<UserInputReader>();
            Bind<UserInputReader>().ToSelf().InSingletonScope();
        }
    }
        static void Main(string[] args) 
        {
            IKernel ninject = new StandardKernel(new MasterEngineModule());
            MasterEngine game = ninject.Get<MasterEngine>();
            game.Run();
        }
 public sealed class UserInputReader : IInputReader
    {
        public static readonly IInputReader Instance = new UserInputReader(Constants.DEFAULT_KEY_MAPPING);
        // ...
        public UserInputReader(IDictionary<ActionInputType, Keys> keyMapping)
        {
            this.keyMapping = keyMapping;
        }
}
If I make that constructor private, it breaks. What am I doing wrong here?
© Stack Overflow or respective owner