Well, I have a dependent property defined in the base class, and I'm trying to use it inside the constructor of its derived class, but this does not work, the property is displayed as null. Unity resolves the dependent property AFTER resolving the instance with the container. Resolve ();
One alternative is to add the IUnityContainer parameter to my constructor for the MyViewModel class and set the ILogger property for yourself:
public MyViewModel(IUnityContainer container) { Logger = container.Resolve<ILogger>(); }
EDIT: Another @Wiktor_Zychla suggestion is to pass the parameter introduced by the constructor as:
public MyViewModel(ILogger _logger) { Logger = _logger; }
Everything seems to work just fine, but I would have to do this for all of my ViewModels derivatives.
But then I do not use the annotated ILogger dependency in the base class. See my class examples below. The question is, what are my alternatives or what am I doing wrong?
Thanks!
I have a ViewModel base class, for example:
public abstract class ViewModelBase { [Dependency] public ILogger Logger { get; set; } .... }
Then I have a class getting:
public class MyViewModel : ViewModelBase { public MyViewModel() {
And at the entry point of my application, I register my ILogger as singleton and my MyViewModel class:
container.RegisterType<ILogger, MyAppLogger>(new ContainerControlledLifetimeManager()); container.RegisterType<MyViewModel>();