MVC controller vs. out-of-box Sitecore Controller
Asked Answered
S

2

5

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.

Salina answered 19/2, 2013 at 12:25 Comment(0)
J
11

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)

Jacinda answered 21/2, 2013 at 9:57 Comment(0)
J
7

My personal view is that the business logic should go in command and query classes that are invoked from the controller class. From these calls you can assemble a strongly typed view model which gets passed to a dumb razor view for rendering.

Ensure that any services that the controller relies on are passed into it via constructor injection using contracts (interfaces) instead of concrete classes and you should end up with solution that is unit testable.

Jerry answered 20/2, 2013 at 9:8 Comment(3)
Thank you Kevin Obiee, could you please help elaborate first paragraph i.e. command query classes == Business Logic ? Any example you could share even pseudo code will helpSalina
The intent is to keep the controllers from becoming a dumping ground for business logic. Jimmy Bogard has blogged extensively on the subject of thin controllers, his recorded presentations are both educational and entertaining. The command query segregation principle is worth honouring, you'll end up with a loosely coupled and intelligible architecture.Jerry
Command Query separation - en.m.wikipedia.org/wiki/Command%E2%80%93query_separationJerry

© 2022 - 2024 — McMap. All rights reserved.