XNA 和 Ninject:依赖参数的语法?

发布于 2024-08-27 14:14:52 字数 399 浏览 4 评论 0原文

我有一个带有公共构造函数的类:

    public MasterEngine(IInputReader inputReader)
    {
        this.inputReader = inputReader;

        graphicsDeviceManager = new GraphicsDeviceManager(this);
        Components.Add(new GamerServicesComponent(this));
    }

如何在仍然提供参数 this 的同时注入 graphicsDeviceManagernew GamerServicesComponent 等依赖项?

I have a class with a public constructor:

    public MasterEngine(IInputReader inputReader)
    {
        this.inputReader = inputReader;

        graphicsDeviceManager = new GraphicsDeviceManager(this);
        Components.Add(new GamerServicesComponent(this));
    }

How can I inject dependencies like graphicsDeviceManager and new GamerServicesComponent while still supplying the argument this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

淡笑忘祈一世凡恋 2024-09-03 14:14:52

您应该能够为组件而不是实际组件注入工厂委托。 NInject 通过其 Bind<>().ToMethod() 支持开箱即用的绑定委托。

这种构造的好处是,您可以获得构造函数注入的好处,同时使实例(本例中为 MasterEngine)能够控制实例化依赖项的时间。 。

您的构造函数应该如下所示:

public MasterEngine(IInputReader inputReader, 
    Func<MasterEngine,GraphicsDeviceManager> graphicsDeviceFactory,
    Func<MasterEngine,GamerServicesComponent> gamerServicesFactory)
{
    this.inputReader = inputReader;

    graphicsDeviceManager = graphicsDeviceFactory(this);
    Components.Add(gamerServicesFactory(this));
}

这是绑定工厂委托的方式,在我看来这是一种更简洁的方法:

Bind<Func<MasterEngine, GraphicsDeviceManager>>()
   .ToMethod(context => engine => new GraphicsDeviceManager(engine));
Bind<Func<MasterEngine, GamerServicesComponent>>()
   .ToMethod(context => engine => new GamerServicesComponent(engine));

这也可以通过委托类型来完成:

public delegate GraphicsDeviceManager GdmFactory(MasterEngine engine); 
public delegate GamerServicesComponent GscFactory(MasterEngine engine); 

...

Bind<GdmFactory>()
   .ToMethod(context => engine => new GraphicsDeviceManager(engine));
Bind<GscFactory>()
   .ToMethod(context => engine => new GamerServicesComponent(engine));

...

public MasterEngine(IInputReader inputReader, 
    GdmFactory graphicsDeviceFactory,
    GscFactory gamerServicesFactory)
{
    ...
}

You should be able to inject factory delegates for the components instead of the actual components. NInject supports binding delegates out-of-the-box via its Bind<>().ToMethod().

The nice thing about such a construct is that you get the benefits of constructor injection while at the same time enables the instance (MasterEngine in this case) to control when dependencies are instantiated.

Your constructor should look something like this:

public MasterEngine(IInputReader inputReader, 
    Func<MasterEngine,GraphicsDeviceManager> graphicsDeviceFactory,
    Func<MasterEngine,GamerServicesComponent> gamerServicesFactory)
{
    this.inputReader = inputReader;

    graphicsDeviceManager = graphicsDeviceFactory(this);
    Components.Add(gamerServicesFactory(this));
}

Here's how you bind the factory delegates, which imo is a tidier way:

Bind<Func<MasterEngine, GraphicsDeviceManager>>()
   .ToMethod(context => engine => new GraphicsDeviceManager(engine));
Bind<Func<MasterEngine, GamerServicesComponent>>()
   .ToMethod(context => engine => new GamerServicesComponent(engine));

This can also be done with delegate types:

public delegate GraphicsDeviceManager GdmFactory(MasterEngine engine); 
public delegate GamerServicesComponent GscFactory(MasterEngine engine); 

...

Bind<GdmFactory>()
   .ToMethod(context => engine => new GraphicsDeviceManager(engine));
Bind<GscFactory>()
   .ToMethod(context => engine => new GamerServicesComponent(engine));

...

public MasterEngine(IInputReader inputReader, 
    GdmFactory graphicsDeviceFactory,
    GscFactory gamerServicesFactory)
{
    ...
}
迷离° 2024-09-03 14:14:52

这个博客建议了一种提供< code>GameServices 反过来又允许注入诸如 GraphicsDeviceManager 之类的东西。我最近使用与此类似的方法实施了一个项目,并且效果很好。

This blog suggests one method of providing GameServices which in turn allow things like the GraphicsDeviceManager to be injected. I have recently implemented a project using an approach similiar to this and it worked a treat.

凹づ凸ル 2024-09-03 14:14:52
kernel.Bind<IInputReader>()
                    .To<SomeInputReader>()
                    .OnActivation(instance =>
                                    {
                                        instance.GraphicsDeviceManager = new GraphicsDeviceManager(instance);
                                        Components.Add(new GamerServicesComponent(instance));
                                    });
kernel.Bind<IInputReader>()
                    .To<SomeInputReader>()
                    .OnActivation(instance =>
                                    {
                                        instance.GraphicsDeviceManager = new GraphicsDeviceManager(instance);
                                        Components.Add(new GamerServicesComponent(instance));
                                    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文