It is possible to inject interface via constructor:
private readonly IDataService _dataService;
public MainViewModel(IDataService dataService)
{
_dataService = dataService;
}
Above injection is proper if service is created in IoC container and never changes.
Another way is to inject via property (property injection)
public IDialogService DialogService
{
get
{
return SimpleIoc.Default.GetInstance<IDialogService>();
}
}
Such solution is useful if the instance of IDialogService may changed during application lifetime.
Then I could change some data:
DialogService.SomeData = UpdatedData
The first way injection is done is easy to test.
I can mock Interface and inject via constructor.
I would like to know what is good practise to ensure above second way is testable.
I would like to ensure that all depended ViewModels using same IDialogService (being changed here in MainViewModel) has up to date same instance.