I wasn't involved in the decisions on this, but I suspect that one reason that the
RegisterViewModel("ABCView", typeof(ABCViewModel))) instruction is needed is because there may be more than one ViewModel that applies to a specific schema and we need to know the relationship between the View and the ViewModel.
For example, an Article Schema may have three fields: Field A, Field B and Field C. Depending on what the specific View does, you may then have two ViewModels that are annotated to work with this Schema:
- ViewModelOne uses Field A and Field B from the Article Schema and also supplements this with some additional information (e.g. from a separate product system). This works with ViewX.
- ViewModelTwo uses Field A and Field C from the Article Schema and contains additional information about these (e.g. it holds additional information about the image in Field C). This works with ViewY.
In this scenario, even through you have semantic mapping on the ViewModel (telling it which schema it relates to), you still need the following to identify which ViewModel to use with which View:
RegisterViewModel("ViewX", typeof(ViewModelOne))); //Relates to the Article Schema
RegisterViewModel("ViewY", typeof(ViewModelTwo))); //Also relates to the Article Schema
Another possible reason (although I've never tested this scenario and am not 100% sure it would work!) is that each Component Template can be associated with more than one Schema and each of these schemas may have a different ViewModel.
For example, we may have a Notification Banner Component Template that has a NotificationBannerView View and is associated with two Schemas: News Article and Press Release.
Each of these Schemas may have a different ViewModel: NewsViewModel and PressReleaseViewModel. In this scenario, you need the
RegisterViewModel commands to be as follows:
//The framework would know which option to choose of these two based on the schema used.
RegisterViewModel("NotificationBannerView", typeof(NewsViewModel))); //Used with the 'News Article' Schema
RegisterViewModel("NotificationBannerView", typeof(PressReleaseViewModel))); //Used with the 'Press Release' Schema
(As I say, I've not tested whether the 'plumbing' for this scenario actually works in DXA, but it would be another reason for needing to use the