Estoy usando Ninject 2.0 para el framework .Net 3.5. Estoy teniendo dificultades con el enlace de singleton.Ninject: ¿sintaxis obligatoria de Singleton?
Tengo una clase UserInputReader
que implementa IInputReader
. Solo quiero que se cree una instancia de esta clase.
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;
}
}
Si hago ese constructor privado, se rompe. ¿Qué estoy haciendo mal aquí?
Algunas variaciones interesantes en embarazos únicos: http: // w ww.yoda.arachsys.com/csharp/singleton.html – mcliedtk
Puede hacer que el constructor sea interno en lugar de privado si están en el mismo ensamblaje. Quizás eso admita un poco de seguridad si le preocupa el código de otros ensambles que acceden a ese constructor. – jpierson
'Bind(). Para (). InSingletonScope()' –
Jaider