0
votes

having problem with my Ninject construct. May be somebody can show me where I am doing it wrong..

ok.. here is Module I have:

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>();
            }
        }

Then in NinjectWebCommons.cs I have:

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;
        }

then I use the property injection: https://github.com/ninject/ninject/wiki/Injection-Patterns

in my "public class TranscriptPageTranscriptWidgetViewModelForWebPage : ITranscriptRendererWidget"

here it is:

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

but, when I am going into the constructor and trying to use _transcriptProvider it is NULL:

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

Any ideas what I am doing wrong? thanks! Al

1

1 Answers

0
votes

Looks like you're trying to access the property within the constructor.

.NET's object creation semantics are such that this simply cannot be made to work (which is one of lots of good reasons to try very hard to achieve things with constructor injection unless you really are dealing with an optional dependency)