DEV Community

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

 
dmueller78 profile image
Dave Mueller

@seangwright I was able to get it working that way. The only thing I haven't been successful at is passing the page into the component as a parameter in the vc: tag... something I'm missing with the "pattern matching" I think. But I was able to use the alternative approach you suggested and get the page context from within the view component itself by way of the IPageDataContextRetriever. Thanks again for sharing your insight on the PTVC pattern and helping me along the way to implementing it. Cheers! -Dave

Thread Thread
 
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