DEV Community

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

 
seangwright profile image
Sean G. Wright

Dave,

Glad to hear you've been evaluating Kentico Xperience! It's a great platform!

The Dancing Goat project is really meant as a feature demo, and not necessarily an architectural/developer guidance project.

Check out this blog post to understand some of the nuance of the different project examples available out there:

devnet.kentico.com/articles/kentic...

If you are looking for a kitchen sink approach that will get you up and running fast (or you don't feel comfortable designing your own patterns and project internals), check out the Kentico Xperience 13 Baseline, maintained by my fellow MVP Trevor:

devnet.kentico.com/articles/get-st...

I don't have any good examples online at the moment of PTVC, but I could definitely provide an example (through a GitHub repo or Gist) that should point you in the right direction.

The key to PTVC is to take what you would have done in a Controller Action and move it to a View Component.

Then, in your Page Template, call that View Component to perform the business logic.

If the View Component needs context about the current Page to query for the right data from the database you can either pass that as a parameter to the View Component from the Page Template using pattern matching to get the correct type:

@* AboutUs Page Template *@
@if (Model.Page is not AboutUsPage aboutUs)
{
  return;
}

<vc:about-us-page page="aboutUs" />
Enter fullscreen mode Exit fullscreen mode

Or you can use the IPageDataContextRetriever in your View Component to get the current Page's information.

Does this help?

Thread Thread
 
dmueller78 profile image
Dave Mueller

That's very helpful, Sean. Thank you for the response with the links and clarifications. I'll give it a go! -Dave

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