Ninject:单例绑定语法?

发布于 2024-08-27 14:09:51 字数 1205 浏览 5 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香橙ぽ 2024-09-03 14:09:51

当然,如果你将构造函数设置为私有,它就会崩溃。您不能从类外部调用私有构造函数!

你正在做的正是你应该做的。测试一下:

var reader1 = ninject.Get<IInputReader>();
var reader2 = ninject.Get<IInputReader>();
Assert.AreSame(reader1, reader2);

您不需要静态字段来获取实例单例。如果您使用 IoC 容器,则应该通过该容器获取所有实例。

如果您确实想要公共静态字段(不确定是否有充分的理由),您可以将类型绑定到其值,如下所示:

Bind<UserInputReader>().ToConstant(UserInputReader.Instance);

编辑:指定要在 UserInputReader 的构造函数中使用的 IDictionary,可以使用 WithConstructorArgument 方法:

Bind<UserInputReader>().ToSelf()
     .WithConstructorArgument("keyMapping", Constants.DEFAULT_KEY_MAPPING)
     .InSingletonScope();

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:

var reader1 = ninject.Get<IInputReader>();
var reader2 = ninject.Get<IInputReader>();
Assert.AreSame(reader1, reader2);

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:

Bind<UserInputReader>().ToConstant(UserInputReader.Instance);

EDIT: To specify the IDictionary<ActionInputType, Keys> to use in the constructor of UserInputReader, you can use the WithConstructorArgument method:

Bind<UserInputReader>().ToSelf()
     .WithConstructorArgument("keyMapping", Constants.DEFAULT_KEY_MAPPING)
     .InSingletonScope();
孤城病女 2024-09-03 14:09:51

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文