With RegisterViewModel(viewName, viewModelType)
, you associate a View with a (Strongly Typed) View Model.
You could argue that this association is already done in the Razor View itself. That is correct and DXA.NET can even infer those associations by compiling all Views, but this adds considerably to the web app startup time. That is why explicit View (Model) registration was introduced in DXA.NET 1.1.
The View Model type mapping flow is as follows:
- CM Pages and Component Presentations have Page Templates and Component Templates, respectively.
- DXA-based Page/Component Templates define a Page/Entity View name (in their metadata)
- DXA-based Page/Entity Views are associated with Page/Entity View Models (through View Model registration)
- This is how DXA can determine Page/Entity View Model types when building a Strongly Typed View Model for a CM Page containing Component Presentations (or for a separately published Dynamic Component Presentation)
So, you see that Semantic Mapping is not involved at all in resolving the View Model types. There is one (advanced) View Model type mapping scenario where the Semantic Mapping is used: so-called polymorphic mapping of linked Components.
If you are mapping a linked Component to an embedded Entity Model, the model mapping already knows the type of the embedded Entity Model (from the Model’s property type), but it is possible to let this be an (abstract) base class and let the model mapping instantiate a concrete subclass. This is called polymorphic mapping and the model mapping logic uses the Semantic Mapping between CM Schemas and Entity Model types to determine which concrete subclass to instantiate.
For example, you see that the Core Module’s Teaser.Media
property is of type MediaItem
, which is an abstract base class. The model mapping will instantiate one of the concrete subclasses (Image
, YouTubeVideo
or Download
), depending on the CM Schema of the linked Component.
Oh, and back to your question why DXA doesn’t use the Semantic Mapping to resolve all Entity Model types: there can be multiple Entity Model types for a single CM Schema. DXA needs to resolve both an Entity View and an Entity View Model type and the View is the most specific. That is why DXA-based Component Templates are associated with a View (which implies a View Model type).