DEV Community

Discussion on: Kentico Xperience Design Patterns: MVC is Dead, Long Live PTVC

 
seangwright profile image
Sean G. Wright • Edited

Dave,

The key to the pattern matching is to 'exit early' if the type isn't correct. That was C# will know that in the rest of the View, the type is what you expect

@model ComponentViewModel<YourPageTemplatePropertiesType>

@if (Model.Page is not YourPageType myPage)
{
    return;
}

<!-- from here on, C# knows Model.Page is YourPageType and myPage is safely typecast as YourPageType -->
<vc:your-view-component page="myPage" props="Model.Properties" />
Enter fullscreen mode Exit fullscreen mode
public class YourModelTypeViewComponent : ViewComponent
{
     public IViewComponentResult Invoke(YourPageType page, YourPageTemplatePropertiesType props)
     {
          // ....
     }
}
Enter fullscreen mode Exit fullscreen mode