I’ve been using Unity for some time in my project. I have a singleton Container which I use to register types and/or instances, and later resolve.
today i wanted to automate some of the instantiation by using property or constructor injection.
I started with Logger class. In Application start i have this code:
RegisterType<Logger, Logger>();
than in my ViewModel
[Dependency]
public Logger MyLogger {
get;
set;
}
here is how i instantiate the viewmodel that has this property (in MainWindow.xaml.cs)
private void InitializeViewModel() {
_vm = new MainViewModel(MainGrid);
...
MyContainer.GetInstance().Container.RegisterInstance<MainViewModel>(_vm);
I can’t get that [property injector] to work. Does property injection NEED to be paired up with a constructor? I am already using a constructor that has some parameters..
Something’s wrong in your example. If you want the
Loggerto be injected intoMainViewModel, you’d have to let the container create theMainViewModelfor you.However, in your code you are creating it by yourself. As I look at it, it should be:
or at least