Ninject工厂扩展和InCallScope没有给出预期的结果 [英] Ninject factory extension and InCallScope does not give expected results

查看:95
本文介绍了Ninject工厂扩展和InCallScope没有给出预期的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用Ninject的工厂扩展名.

I am struggling with using the factory extensions for Ninject.

将扩展名与InCallScope结合使用时,我希望从工厂的create方法返回相同的实例,但是我得到了两个不同的实例.

When using the extension in combination with InCallScope, I expected the same instance to be returned from the factory's create method, but instead I get two different instances.

我是否误解了InCallScope概念,还是需要添加其他内容?

Have I misunderstood the InCallScope concept or do I need to add something else?

using System;
using Ninject;
using Ninject.Extensions.Factory;
using Ninject.Extensions.NamedScope;

namespace MyTest
{
    class Program
    {
        static void Main()
        {
            var kernel = new StandardKernel();

            kernel.Bind<IMyFactory>().ToFactory();

            // Does not give what I want, not a surprise...
            // kernel.Bind<IMyStuff>().To<MyStuff1>().InTransientScope();

            // Works - of course, but I don't want a singleton. It should only be used in call scope...
            // kernel.Bind<IMyStuff>().To<MyStuff1>().InSingletonScope();

            kernel.Bind<IMyStuff>().To<MyStuff>().InCallScope();

            var myFactory = kernel.Get<IMyFactory>();

            // Creating my first instance...
            var myStuff1 = myFactory.Create();

            // Creating my second instance...
            var myStuff2 = myFactory.Create();

            // 
            if (myStuff1.SeqNo != myStuff2.SeqNo)
                throw new Exception("Except them to be equal...");
        }
    }

    public interface IMyFactory
    {
        IMyStuff Create();
    }

    public interface IMyStuff
    {
        int SeqNo { get; }
    }

    public class MyStuff : IMyStuff
    {
        private static int _staticSeqNo;

        public MyStuff()
        {
            SeqNo = _staticSeqNo;

            _staticSeqNo++;
        }

        public int SeqNo { get; private set; }
    }
}

推荐答案

Ninject.Extensions.ContextPreservation是您所遇到的情况的缺失链接-当其模块安装在内核中时,它将使行为更改为您期望的样子.

Ninject.Extensions.ContextPreservation is the missing link for your case - when its Module is installed in a Kernel, it will change the behavior to what you expect.

但是等等...它们单独的调用-如果不是,它们会变得非常混乱(没有引用,但与@Remo Gloor进行了非正式讨论).因此,Unstable NuGet软件包(在撰写本文时)更改了行为,使其可以正常工作.

But wait... They are separate calls - stuff gets very confusing if they are not (no citations, but discussed with @Remo Gloor informally). For this reason, the Unstable NuGet packages (as of this writing) change the behavior to work as you have.

这个问题和我的两个答案应该可以说明问题,并希望您能找到解决方案. 寻找行为类似于InRequestScope的Ninject范围

This question and my two answers should illustrate the problem and hopefully move you towards a solution. Looking for a Ninject scope that behaves like InRequestScope

您的任务,如果您选择接受,那就是将此信息传播到Wiki文章中,该文章使您(和我)感到困惑,期望其他行为.

Your mission, should you choose to accept it is to spread this info into the wiki article that confused you (and me) into expecting the other behavior.

这篇关于Ninject工厂扩展和InCallScope没有给出预期的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆