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

Using Sitecore Users in your unit tests

Hyper quick post for the morning ๐Ÿ˜€ – I have once again been tasked with security related work in Sitecore. I know there are many approaches to this – wrapping, Virtual Users (which requires dummy config) , but on this occasion (have done it a few times before too) I am not particularly interested in interrogating the user once I have finished with it – just ‘mocking’ / stubbing one as an argument or a return. So I thought I use this:

    public class TestUser : User
        public TestUser(string name, bool isAuthenticated)
            : base(name, isAuthenticated)


Works a treat ๐Ÿ˜€

Thoughts from an opinionated developer: API design, mocking and sitecore

A few thoughts for the weekend in my usual opinionated way. Please take these with a measure of whimsy!

Lets kick things off and poke about with the topic of API design. Now if we’re fair this topic has been done to death. That said, any topic like this is well worth re-visiting too. In any form of programming, there are always going to be best practises but our application of these should always be on a ‘well it depends’ nature.

We see this quite often in how an API is designed. Good practise tells us that it should be SOLID but the steps to achieving that in the confines of your
project can vary quite a bit. Its here that rigid adherence to ‘best practise’ without allocating a good helping of common sense and experience can stunt or even ruin an API for general use.

Continue reading

Don’t Fight The Framework – Further down the rabbit hole optimising Sitecore MVC Performance.

I figured I had put this little mini-series too bed with my posts on Strategies to optimise Sitecore MVC performance and Further strategies to optimise MVC performance, it seems not.

I was recently looking into the effect of the @Html.Sitecore().Controller(“”) @Html.Sitecore().Rendering(“{id}”) on the performance of a Sitecore MVC based solution. Here is what I found:

Continue reading