We use lots of interfaces and constructor injection in our application. We have default implementations of the interfaces registered with Unity and for the most part unity will fill the constructor dependency with the objects based on their lifetime.
One example we have is a MessageBoxView that takes in it's constructor a IMessageBoxViewModel.
But if I want to change the messageboxviewmodel to a different message I can do this.
Using this I can do dependency overrides when needed or if we have interfaces in the constructor to swap out implementations depending on the application state.
Also with Unity you can send in overrides and if the object graph doesn't need them they are just ignored. So in the CreateNewRegionItem of the RegionNavigationContentLoader we always pass in the UriQuery. If one of our classes need it we can just add it to the constructor and get access to it. That's not part of the stackoverflow post. I added that in later.
This allows for us to pass information to the viewmodels or views. It doesn't have to replace the entire viewmodel it would be whatever in the unity object graph that unity would be newing up when resolving a view..
One example we have is a MessageBoxView that takes in it's constructor a IMessageBoxViewModel.
public interface IMessageBoxViewModel
{
string[] Choices { get; set; }
string Title { get; set; }
string Content { get; set; }
}
public MessageBoxViewModel : IMessageBoxViewModel
{
public MessageBoxViewModel()
{
//default choices
Content = "Continue?";
Choices = new [] {"Ok","Cancel"};
}
public string[] Choices { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}
If I use the RequestNavigate and pass in the Uri to the MessageBox I'm going to get a messagebox with Ok & Cancel with the content saying "Continue?". Because unity will resolve a new messageboxviewmodel. But if I want to change the messageboxviewmodel to a different message I can do this.
// Technically I use unity to resolve messageboxviewmodel but it doesn't matter for this exercise.
IMessageBoxViewModel messageBoxViewModel = new MessageBoxViewModel();
messageBoxViewModel.Content = "Save changes?";
messageBoxViewModel.Choices = new [] {"Yes", "No", "Cancel"};
From the code from stackoverflow link I sent I can do var navUri = new NavigationUri("MessageBoxView");
navUri.Add(messageBoxViewModel );
RequestNavigate("Popup", navUri);
This will create a new MessageBoxView but instead of creating a new MessageBoxViewModel unity will inject the messageBoxViewModel object I created. Using this I can do dependency overrides when needed or if we have interfaces in the constructor to swap out implementations depending on the application state.
Also with Unity you can send in overrides and if the object graph doesn't need them they are just ignored. So in the CreateNewRegionItem of the RegionNavigationContentLoader we always pass in the UriQuery. If one of our classes need it we can just add it to the constructor and get access to it. That's not part of the stackoverflow post. I added that in later.
This allows for us to pass information to the viewmodels or views. It doesn't have to replace the entire viewmodel it would be whatever in the unity object graph that unity would be newing up when resolving a view..