My tiny, novice, Bootstrapper
configures the container as follows:
protected override void ConfigureContainer()
{
base.ConfigureContainer();
RegisterTypeIfMissing(typeof(IUserSettingsProvider), typeof(JsonUserSettingsProvider), true);
RegisterTypeIfMissing(typeof(IAppState), typeof(AppState), true);
Container.RegisterType<TripleDesEncryptionService>(new InjectionConstructor(App.CryptoKey));
Container.RegisterType<BaseViewModel>(new InjectionConstructor(Container.Resolve<IAppState>()));
}
and my BaseViewModel
has only one ctor:
public abstract class BaseViewModel : BindableBase, INotifyPropertyChanged
{
protected BaseViewModel(AppState appState)
{
AppState = appState;
}
...
}
When I try and run the project, the bootstrapper fails on RegisterType<BaseViewModel>
and throws the following exception.
System.InvalidOperationException: 'The type ApptEase.Client.Infrastructure.ViewModels.BaseViewModel does not have a constructor that takes the parameters (AppState).'
AppState
is created and registered with the container in the application startup code:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var boot = new Bootstrapper();
boot.Run();
Container = boot.Container;
_appState = new AppState();
Container.RegisterInstance(typeof(AppState), _appState);
}
Your BaseViewModel class constructor is protected. Which is not visible to Unity. But anyway, as user 3615 wrote in comments -
You cannot resolve abstract class since instance cannot be created
So I would suggest explicitly registering a concrete implementation instance and mapping it to that base type.
Container.RegisterInstance(typeof(BaseViewModel), new MyConcreteViewModel());
But in the end what you want is actually an interface typed registration. That unlocks testability via mocking.