Blog

Best Practices on Project Structure with the Sitecore Solution

The main difference between Sitecore MVC and traditional .NET MVC is that Sitecore MVC works with ASP.NET Web Forms side by side and you can switch between them from request to request. This sort of a technology blend results in the ability to use both MVC and Forms on different pages of your application without plugging one into another. You can actually mix them together even without Sitecore. And that is something that could have been expected since the ASP.NET MVC framework was built on top of ASP.NET.

The diagram below shows the execution of an ASP.NET MVC application.

Whenever a request appears, it is routed to a controller; after that, the action is invoked which in turn activates a method. In this method, a developer usually creates a model and passes it along to the view; then, HTML is rendered back to the browser.

The diagram below depicts how Sitecore handles a request.

When a request appears, it goes straight to the HTTP request pipeline. The pipeline determines whether this request matches a standard MVC route or not. If yes, it is handled as an MVC route in an MVC application. If no, Sitecore will try to resolve that particular item’s layout. When there is a corresponding layout, then the next question will be whether the layout matches the MVC extension settings or not. If the answer to this question is “yes”, then it is indeed an MVC request. Otherwise, it is a web form request.

However, if there is no specified layout and the controller is mentioned, a controller field of an item gets analyzed. In case the controller is found, an MVC request is processed as usually – going to action. If not, the only option left is to assume it is a web form request and to return a ‘layout not found’ error.

The other two main differences between ASP.NET MVC and Sitecore MVC are the following ones:

          • Sitecore pages are constructed based on components. If web forms are used, these components are sublayouts or *.ascx files.
          • Sitecore MVC has view contain renderings. These are MVC views in its foundation. A page can consist of multiple view renderings nicely forming the layout together without a controller. There is also an option to have a controller rendering where you can specify which view to return.

So, as you can see the Model-View interaction is executed multiple times per one-page load. It is different from ASP.NET MVC.

Best Sitecore Project Structures

When we talk about the project structure inside the Sitecore CMS, first of all, we should keep in mind that we build the solution to use and consume. Then, it is important to define the concept, organize the information, structure, model and, finally, use it.
Usual Sitecore content is:

            • Web content – articles, news, events, pages
            • Structural data – products, personal files
            • Metadata – anything that describes or classifies any of the above-mentioned things.

These are the points to consider while structuring your solution.

Use of Templates

Designing a template keep in mind that it is always best to have a set of templates that will be reused for every project.

When you structure templates, consider the following recommendations:

            1. Create your own template base which will inherit from the standard base template. If you need to make a sweeping change later, this will provide a place to intercept and add another layer.
              Create your own template base which will inherit from the standard base. If you need to make a sweeping change later, this will allow intercepting and adding another layer.
            2. Usually, you need only one Page template. The page basically inherits from metadata, navigation and some other stuff located on every single page of your application.
            3. The content templates: create the sampling of the majority of the commonly used fields that you will use in most of your content (your article pages, carousels, navigation). These are all content modules and building blocks for pages.
            4. Create your page templates using inheritance without defining your own fields. Inherit from Base, a separate template like, for example, “headline”, “subtitle”, “RichContent”, etc. This approach will result in a lot of benefits. If you need to change anything, you can do that easily from one place.

All this makes your items and Model really simple and flexible.

Use of Standard Values

Sitecore Standard Values is a great feature, mainly used for page templates. It allows creating a page template and defining its pre-built modules. Whenever you create a page based on this template, you will have all the Master Data like Header, Footer, Doormat and other navigation included in your Page layout.

Usage of DataSources

Data Sources in Sitecore are interesting in a couple of ways:

            • It is a great place to let the Content Editors manipulate the css layout of the page. You can have a name-value collection of css classes and their aliases stored as Additional Parameters. And then, act accordingly on your view.

            • This is also a place to specify the placeholder that will hold your view rendering layout.
            • And the main purpose, of course, is to link the Data Item and help to construct the model (based on this data) in the back-end. Not to mention – to provide Content Editor with the ability to insert, edit and delete data by means of CMS.

Usage of View Renderings, Controllers


Instead of using sub-layouts in Sitecore MVC, controller renderings and view renderings are used. Mostly, controller renderings; though, View rendering are used if you want to take raw Sitecore data and display it somehow. In a view rendering, Sitecore acts as a controller.

Mapper to Use

“Glass Mapper is the way to go when choosing a Sitecore ORM. Not only it is easy to use, flexible and well integrated with Code Generation, but it also cuts development time in half.”
– Nikola Gotsev Sitecore MVP

Glass.Mapper is an open source mapping framework that helps to get your focus back on solving business tasks. It converts data from your CMS to classes that you can reference in your code.
By means of Glass.Mapper plugged into Sitecore, you get strongly typed models in your C# code. Just install, plug it in, regenerate classes and reference them in your Views, Models etc. Of course, these generated classes are easily extensible. Just create another partial class and enrich it to the perfect state.

Few benefits of Glass.Mapper:

            • Adds unit testability
            • Adds easy configuration to map third-party classes in third-party assemblies
            • Auto data conversion
            • Extensible
            • Customizable pipelines
            • Works with IOC.

Using NuGet, you can easily install the GlassMapper.Sc.Mvc package and update it whenever the new version is released.

All set up to plug it into your view. Here is a little example:

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<CompanyName.Corporate.Web.GlassModels.sitecore.templates.CompanyNameTemplates.Corporate.Components.TContent_Description>

@using Sitecore.Mvc

@using Sitecore.Mvc.Presentation

@using System.Data.Linq

@using Sitecore;

@using Sitecore.Data.Items;

                            

                               <h4>@Editable(Model, x => x.Title)</h4>

                               @Editable(Model, x =>  x.Summary)  

                               @if (Model.StTextLinks != null)

                               {

                                  <div class=“info-item-block”>


                                    <ul class=“link-list”>


@foreach  (var Lnk in Model.StTextLinks)

                                    {

                                          <li>@RenderLink(Lnk, x => x.Link, isEditable: true)</li>

                                    }

                                  </ul>

                               </div>

                        }

The great thing about using Glass Mapper is that you can end up having the simplest possible Page Editor Mode. And people with as little as Microsoft Office experience can edit, insert and enrich your content. This great thing comes with the Editable helper method to which you can pass some standard types of data to render.

TDS

Team Development for Sitecore (TDS) is a tool that synchronizes Sitecore items and integrates them into your solution project in Visual Studio. It also has the capabilities of a Source Control Management System fully integrated into your Solution and Sitecore Development environment. Keeping all items as files (xml-based files) allows you to put them into your Source Control System and share across team members and different environments like Staging or Production.

Using TDS as a part of Continuous Integration means that all code and Sitecore items are deployed to the targeted server automatically. If you choose not to use Continuous Integration (Production deployment, for example) you can let Team Development for Sitecore create an Update Package for you.

And that is essentially what every development team wants for their project.

However, TDS still has a couple of annoying bugs that will pop up every now and then. Not to mention that it is an expensive tool.

Free Solution Unicorn as a Less Sophisticated Alternative to TDS


Unicorn is a project by @kamsar. Unicorn implements an event handler to serialize modified Sitecore items to a disk.

A unicorn is a free tool for Sitecore developers that helps to move database items, templates, renderings into a different Sitecore instance. The tool comes in very handy when developers try to merge or share the work that is done in their own local instances. Unicorn saves serialized copies of Sitecore items to a disk; then, developers can use source control to manage the state and updates of Sitecore items.

Unicorn uses a Control panel as a tool with a little interface. The Control panel is located on the page named unicorn.aspx. On this page, one can synchronize Sitecore items with the previously saved copies on the disk. Synchronization occurs when one is trying to get the recent changes from source control.

Unlike TDS, Unicorn doesn’t offer item selection for manual synchronization. Mostly, selecting items is an annoying feature of TDS but it also provides some sort of flexibility. The feature is useful when you want to keep some items from releasing for some time or have a stable version without unwanted changes.

 

EXTEND YOUR ENTERPRISE WITH TOP
CLASS TECH TALENT

contact us