Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC controller vs. out-of-box Sitecore Controller

I've been reading a lots of blogs on MVC provided here: http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog.aspx

However, I am not being able to explain/convience myself/team:

  1. When to use custom control vs. out of box site core controller?
  2. When does the Out of Box Controller gets invoked?
  3. Benifit of custom control vs. out of box controllers?
  4. If we go with out of box, should we include all business logic on Views. Is this testable?

I also looked at below and still not certain: https://bitbucket.org/demoniusrex/launch-sitecore-mvc-demo

Any help will be appreciated.

like image 700
Nil Pun Avatar asked Feb 19 '13 12:02

Nil Pun


1 Answers

Whilst I broadly agree with Kevin Obee's statement I think it's worth reminding ourselves that controllers are being used in two distinct roles in Sitecore:

  • Page level controller (invoked by item route)
  • Component level controller (invoked by redering mechanism)

When to use: Custom controller / default Sitecore controller

Page level controller

Any route that matches an item path will by default use the Index action on the Sitecore.Mvc.Controllers.SitecoreController. This action will return a ViewResult based on the layout configuration of the item.

If you have a need for changing this behaviour (e.g. something that impacts the entire page) you can specify a custom controller and action on the item (or the Standard Values for the item). For the custom controller you can either roll your own or subclass the default controller.

Component level controller

For ViewRendering Sitecore renders the Razor views without the need for a specific controller (well I guess it's the page level controller that is in play - but just imagine that Sitecore provides a default controller that gets the model using the mvc.getModel pipeline and feeds it to the Razor view).

For ControllerRendering you provide a custom controller that can execute logic (see Kevin's answer) and provide a model for the view. There is no benefit from subclassing Sitecore.Mvc.Controllers.SitecoreController.

When are controllers invoked

Page level controller

The action on the page level controller is invoked by the routing engine.

Component level controller

The action on a ControllerRendering is invoked as the page view renders.

Benefit of using: Custom controller / default Sitecore controller

The benefit of a custom controller over the default Sitecore controller is that you are in control of the logic. The benefit of using the default Sitecore controller is that Sitecore provides the logic for you.

Should we include all business logic on Views

No. (See Kevin's answer)

like image 80
herskinduk Avatar answered Nov 29 '22 17:11

herskinduk