.Net 3.5 프레임 워크에 Ninject 2.0을 사용하고 있습니다. 싱글 톤 바인딩에 어려움이 있습니다.Ninject : 싱글 톤 바인딩 구문?
나는 IInputReader
을 구현하는 클래스 UserInputReader
을 가지고 있습니다. 나는 오직이 클래스의 한 인스턴스 만 만들어지기를 원합니다.
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;
}
}
해당 생성자를 비공개로 설정하면 중단됩니다. 여기서 내가 뭘 잘못하고 있니?
몇 가지 흥미로운 변화 : HTTP :
UserInputReader
의 생성자에서 사용하는IDictionary<ActionInputType, Keys>
를 지정하려면WithConstructorArgument
방법을 사용할 수 있습니다 // 승 ww.yoda.arachsys.com/csharp/singleton.html – mcliedtk동일한 어셈블리에있는 경우 private 대신 생성자를 내부로 만들 수 있습니다. 아마도 다른 어셈블리의 코드가 해당 생성자에 액세스하는 것이 염려되는 경우 광고가 약간 안전 할 수 있습니다. – jpierson
'Bind(). (). InSingletonScope()' –
Jaider