0

我的 Ninject 构造有问题。可能有人可以告诉我我在哪里做错了..

好的..这是我拥有的模块:

public class WebPageModule:NinjectModule
        {
            public override void Load()
            {
                Bind<TranscriptPageMediaWidgetViewModelForWebPage>().ToSelf().InSingletonScope();
                Bind<TranscriptPageTranscriptWidgetViewModelForWebPage>().ToSelf().InSingletonScope();
                Bind<WebPageTranscriptProvider>().ToSelf().InSingletonScope();

                Bind<ITranscriptProvider>().To<WebPageTranscriptProvider>().WhenInjectedInto<TranscriptPageTranscriptWidgetViewModelForWebPage>();

                //Bind<ITranscriptProvider>().To<WebPageTranscriptProvider>();
                Bind<ITranscriptRendererWidget>().To<TranscriptPageTranscriptWidgetViewModelForWebPage>();
                Bind<IMediaRendererWidget>().To<TranscriptPageMediaWidgetViewModelForWebPage>();
            }
        }

然后在 NinjectWebCommons.cs 我有:

private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel(new WebPageModule(),new TweeterModule(), new BookmarkModule());
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            kernel.Settings.AllowNullInjection = true;//http://stackoverflow.com/questions/10517962/using-default-parameter-values-with-ninject-3-0

            RegisterServices(kernel);
            return kernel;
        }

然后我使用属性注入: https ://github.com/ninject/ninject/wiki/Injection-Patterns

在我的“公共课 TranscriptPageTranscriptWidgetViewModelForWebPage : ITranscriptRendererWidget”

这里是:

 [Inject]
        public ITranscriptProvider TranscriptProvider
        {
            get { return _transcriptProvider; }
            set { _transcriptProvider = value; }
        }

但是,当我进入构造函数并尝试使用 _transcriptProvider 时,它为 NULL:

 public TranscriptPageTranscriptWidgetViewModelForWebPage(string dataEndpoint, string focusCue)
        {
            InitParentInterfaceProperties();
            Transcript = _transcriptProvider.GetTranscript(new Uri(dataEndpoint));
            FocusCue = focusCue.Replace("*", "").ToLower();
        }

任何想法我做错了什么?谢谢!铝

4

1 回答 1

0

看起来您正在尝试访问构造函数中的属性。

.NET 的对象创建语义使得这根本无法工作(这是努力通过构造函数注入来实现目标的众多充分理由之一,除非您真的在处理可选的依赖项)

于 2012-09-21T22:53:26.860 回答