Ninject:单例绑定语法?
我正在将 Ninject 2.0 用于 .Net 3.5 框架。我在单例绑定方面遇到困难。
我有一个类UserInputReader
,它实现了IInputReader
。我只希望创建该类的一个实例。
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;
}
}
如果我将该构造函数设为私有,它就会崩溃。我在这里做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,如果你将构造函数设置为私有,它就会崩溃。您不能从类外部调用私有构造函数!
你正在做的正是你应该做的。测试一下:
您不需要静态字段来获取实例单例。如果您使用 IoC 容器,则应该通过该容器获取所有实例。
如果您确实想要公共静态字段(不确定是否有充分的理由),您可以将类型绑定到其值,如下所示:
编辑:指定要在
UserInputReader
的构造函数中使用的IDictionary
,可以使用WithConstructorArgument
方法:Of course it breaks if you make the constructor private. You can't call private constructors from outside your class!
What you're doing is exactly what you're supposed to do. Test it:
You don't need the static field to get the instance singleton. If you're using an IoC container, you should get all your instances through the container.
If you do want the public static field (not sure if there is a good reason for that), you can bind the type to its value, like this:
EDIT: To specify the
IDictionary<ActionInputType, Keys>
to use in the constructor ofUserInputReader
, you can use theWithConstructorArgument
method:IInputReader 不声明 Instance 字段,也不能声明该字段,因为接口不能有字段或静态字段,甚至不能有静态属性(或静态方法)。
Bind类无法知道它是在寻找Instance字段(除非它使用反射)。
IInputReader does not declare the Instance field nor can it, since interfaces can not have field or static field or even static properties (or static methods).
The Bind class can not know that it is to find the Instance field (unless it uses reflection).