Did you really mean .Controller() in Sitecore MVC?

Real short post this one, but I have seen this a few times from different developers, so I am now considering that this is a common mistake and can be disastrous for performance.

The mistake is this – developers using code like the following:


@Html.Sitecore().Controller("ControllerName", "MethodName");

This code is fine and dandy IF you want to execute a controller with frills. HOWEVER, the number of times I have seen it used on the Layout to bring in Metadata, Header, Footer or any other statically bound rendering. This WILL BYPASS Sitecore’s output caching mechanisms altogether, so every page of your site (potentially) execute every controller added in this manner.

In the code below, I have simply created a controller rendering in Sitecore (though could be any) and used its ID to display the statically bound rendering using @Html.Sitecore.Rendering().


@Html.Sitecore().Rendering(RenderingConstants.MetaData, new { Cacheable=true, Cache_VaryByData=true })

Note also the EXPLICIT caching, at the time of this post, Sitecore still has an MVC based bug that means the rendering items caching settings are ignored altogether (there is a patch and I hope it will become core)

Continue reading

Glass Mapper V4 – Redis Cache Provider

Following on from my post on the new caching functionality in Glass Mapper V4, I thought I would do something that I have been really dying to try, which is to look at using Redis to cache the models generated out of Glass. This from an architectural standpoint for me is just plain amazing :D. In this post, I will show you how you can set up a redis based cache provider for Glass Mapper V4. This post will assume you know how to set up caching on your models (shown in my previous post).

** Note – this is a proof of concept, it has not yet been tested in production, all the regular no warranty disclaimers apply.

Continue reading

Glass Mapper V4 – Configuration Maps

Something I had a look at quite a while back was the fluent configuration api in Glass. Unlike many developers, I am absolutely 100% fine with adding spaces to field names and don’t like using attribute based configuration in Glass. I talked about this in some detail a while ago in Introducing Glass Maps. I have since been through several iterations and finally settled on one that I like and therefore contributed to Glass itself. In essence, it is simply a neat way of bundling your fluent mapping into discrete units, which allows you to keep the mapping isolated from your POCO’s.

I understand this is not to every developers taste, but if you like to see clean .net objects, this my be the mapping configuration type for you.

Continue reading

Glass Mapper V4 – Caching

Following on in my series looking at the new features in Glass Mapper v4.

One of my favourite features of Glass V4 is the new Caching feature. This at at in its simplest form allows you to cache the object that Glass produces, much like we do in regular .net development for slower running processes. In this post I will describe some more about caching, how to set it up and what it does.

Continue reading

Allowing unit tests using generic funcs, actions or expressions

The other day, I was required during writing a unit test on a Sitecore pipeline to be able to get the value from a method in a base class in the original Sitecore API. In this case it was Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey, and what I needed to do was get the cachekey from the base and then modify it to add additional string values to the key. In doing so I realised that the standard ‘abstract it away’ didn’t really work in its traditional form, since the abstraction would not be aware of ‘base.’. I could pass in that specific method as an argument, but felt that a better approach might be to create a utility repository that would allow me to execute code with an expected return. This meant using a normal Mock of this approach, I could abstract the code in question and simply return what I expect to be the value, without worrying about it (in this case) actually getting the API involved.

Continue reading

Getting a Sitecore Model From The cshtml (View)

As most of you will no doubt be aware, I am an advocate of using an ORM (you decide – I personally like Glass Mapper For Sitecore) in your Sitecore solutions. A little while back the magnificent John West did a great post on a proof of concept for getting a model from the .cshtml rather than defining it in Sitecore. I thought this was awesome and since then I have been experimenting with it as an idea.

I have kept this example based on vanilla Sitecore, however, would take very minor modification to use with an ORM.

Continue reading

Sitecore WebAPI – Hooking into the route setup

Setting up routes in MVC and WebAPI is pretty straightforward in Sitecore provided that its done in the correct pipeline. When I first started attempting to add routes, I quickly discovered that doing this via the Global ASAX wasn’t reliable. The reason for this is that the routes are setup in the initialise pipeline.

Fortunately Sitecore has this process mapped out in the the config file so all I needed to do is swap out the DefaultRouteMapper that comes out of the box. Lets look at the entry in Sitecore:

<setting name="Sitecore.Services.RouteMapper" value="NAMESPACE.CustomRouteMapper, ASSEMBLY_NAME" />

Continue reading

Sitecore WebAPI – Setting up IOC

I’ve had the opportunity to tinker in the depths of the WebAPI in Sitecore 7.5 and 8 recently. I’ll put up a post about setting up controllers easily shortly but until then I thought I’d share how to setup IOC.

In a normal WebAPI environment, we can create our resolver and attach it to the GlobalConfiguration. However, we must remember that Sitecore uses its own pipelines for MVC and WebAPI and so setting this up in the Global ASAX won’t work.

The way I’ve done it is to change the registered WebAPI initialiser class in the Sitecore.Services.Config file found in App_Config/Include. This lets me hook directly into the entry point for Sitecore related WebAPI and adjust the configuration accordingly.

Continue reading