2

My goal is simply this: I want the already existent instance of class "MainWindowVM" (implementing IMainWindowVM) to be injected into class "StaticTestsResultsViewModel". I do not(!) want a new instance of class "MainWindowVM" to be instantiated. Instead, I want the already existent instance of this class to be injected into class "StaticTestsResultsViewModel".

steady_progress
  • 3,311
  • 10
  • 31
  • 62
  • You haven't really provided enough information to know what's going on here, but I can't help but notice that you have many classes with Static in the name, but nothing is actually static. – LordWilmore Jan 25 '18 at 18:41
  • 1
    the application is conducting static analyses of software .... that is why everything is called StaticAnalysisblabla – steady_progress Jan 25 '18 at 18:43
  • 1
    Ok, you need to look at your creation structure, but you might take a look at registering singleton objects in ninject, which is covered in posts such as this: https://stackoverflow.com/questions/21706436/ways-to-setup-a-ninject-singleton – LordWilmore Jan 25 '18 at 18:45
  • I understood the post in such a way that I should do the folowing: kernel.Bind.To().InSingletonScope(); However, this "is not allowed in this context", as visual studio is telling me – steady_progress Jan 25 '18 at 18:58
  • 1
    There is an example of using Ninject with WPF [here](https://stackoverflow.com/a/25524753/181087). Be sure to follow the link at the bottom to the related answer, as that one seems to provide an improvement to the accepted answer that switches from using a service locator to using DI. – NightOwl888 Jan 25 '18 at 19:10
  • @NightOwl thanks for the link ... I will read it ... I have also added a simple sub-question (below "Update") to my post. If I got this answered, it might be enough for me to solve the problem – steady_progress Jan 25 '18 at 19:15
  • @NightOwl I reimplemented the dependency injection according to the post you linked to .... there is no exception occurring but there's still this problem: The property "SACount" of class MainWindowVM should have the value "9" ... but when the method generateMessagesForStaticTestsResultsViewer() accesses it, it has the value "0" instead. If there is only one instance of class MainWindowVM, then its properties must have the same value ... but this doesn't seem to be the case here – steady_progress Jan 25 '18 at 20:38

1 Answers1

1

My goal is simply this: I want the already existent instance of class "MainWindowVM" (implementing IMainWindowVM) to be injected into class "StaticTestsResultsViewModel". I do not(!) want a new instance of class "MainWindowVM" to be instantiated. Instead, I want the already existent instance of this class to be injected into class "StaticTestsResultsViewModel".

In that case you should register it like:

kernel.Bind<IMainWindowVM>().To<MainWindowVM>().InSingletonScope();

Do keep in mind that when you register a class as singleton, this implicitly makes all of its dependencies singleton as well. See Captive Dependency. If that is unacceptable to your application design, you should move the singleton instance where you store the shared property to be a dependency of MainWindowVM and not make MainWindowVM singleton (such as transient).

                          PropertyHolder (Singleton)
                         /
MainWindowVM (Transient)
                        \
                          OtherDependency (Any scope shorter than singleton)
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • kernel.Bind().To().InSingletonScope(); is exactly what I did (see code in File IocConfiguration.cs in my edited post) ... but it didn't work – steady_progress Jan 25 '18 at 21:12
  • Your setup doesn't include the modifications from [this answer](https://stackoverflow.com/a/43614998/181087) that I mentioned before. – NightOwl888 Jan 25 '18 at 21:22
  • yes, because I didn't understand what "Just remove StartUpUri from your App.xaml" was supposed to mean ... so I implemented the version I understood rather than the perfect solution I didn't understand – steady_progress Jan 25 '18 at 21:23
  • 1
    It means remove the `StartUpUri` attribute from the App.xaml (the last attribute on the `Application` element and its value). – NightOwl888 Jan 25 '18 at 21:26
  • i see ... just noticed that .... will implement the perfect solution now – steady_progress Jan 25 '18 at 21:27
  • 1
    1) `YourModule()` is a Ninject module, which is a way to move several related `kernel.Bind` statements into a single chunk. It makes no difference if you do it in a module, or replace that line with all of your `Bind` statements. 2) Yes, as far as that example is concerned. – NightOwl888 Jan 25 '18 at 21:47
  • 1
    3) I located an answer that I previously gave [here](https://stackoverflow.com/a/32552728) that was literally "by the book". It is more complicated, but at least it is all in one piece. I suspect you are going to have an issue with `StaticTestsResultsViewModelLocator` that this provides a workaround for. – NightOwl888 Jan 25 '18 at 22:10
  • I implemented the "perfect solution" now .... behaviour is unchanged (see Update 2 above) – steady_progress Jan 25 '18 at 22:43
  • will have a look at the new link you provided tomorrow ... did I do something wrong when implementing the "perfect solution"? If so, please tell me - then I don't have to reimplement everything from scratch – steady_progress Jan 25 '18 at 22:45