Monday 25 May 2015

Guidelines for Sitecore MVC


­
1.    Controller is supposed to control and direct, it should not have any logic (many developers forget this and it happened in Vision Web too) and should be as slim as possible.
2.    One of the main advantages of MVC is that the view (technically called as View template) is not tightly coupled with the server side. So, we can switch/add a new view (say for mobile or tablet) for an existing flow any time. In order to achieve this we should make the view as ignorant as possible of what it displays and should concentrate more towards how it displays.
3.    Try to avoid c# code in .cshtml as much as possible. Use html helpers in all situations. Create extension methods for html helpers to re-use code and constructs.
4.    Model could contain all logics and calculations and can be fat. It is good to use Model only to represent an entity and to use ViewModel to represent transit entities.
5.    Better to avoid ViewBag or ViewData for transit entities when they are present in multiple places. ViewModel instead would make the code more managable.
6.    Use IOC to decouple the connection to SiteCore. This will help in redirecting the call for test data to our own repository.
7.    Methods which are not Actions should not be public.
8.    Use TempData instead of Session when the data is needed only for the subsequent request.
9.    Use filters as and when it becomes appropriate. It will help in getting an easier control of the flow in different stages on a global scale.
1.    For enhancing reusability, use controls/partial pages as much as possible.
. ...   Exception handling mechanism and Logging framework will have to be put in place which will not require much effort from the developer every time that he has to handle it for his module.

1.  Never put an unnecessary check when some value is not intended to be there. There were many problems in Visionweb due to this. (Example of this is developers putting a check for null/empty everywhere. This habit will avoid the exception but will make the intention of the code ambiguous and will lead to loop holes in the flow.