Skip to Content

Using Mediator Design Patterns for CMS development

Why I think using Mediatr makes sense

After being recommended Mediatr a while back I have been finding more and more use cases for it. the most recent has been for CMS development where the CMS supports 'dynamic areas' such as Content Areas in Episerver (Optimizely), and multi reference fields in Contentful. These has always been difficult as you don't know the type of the child components, and you often find yourself using switch statements and casting or awkward reflection code that can quickly become unmanageable, and a lot of the time I have found a lot of developers put all this code in the controller(s).

Controller code logic
Simple approach, normally involving a switch statement to get the view models for each partial

My approach is to always have nice 'slim' controller, and Mediatr enables this by extracting the page logic into page IRequestHandler<>s which the either uses Automapper or Microsoft Dependency injection to create an IRequest<> for each block on the page (including those in 'dynamic areas'). This means that the page IRequestHandler<> are also pretty light, and the logic for each component has it's own IRequestHandler<> where the logic for each block is kept, giving a very high level of separation.

Mediatr approach
A very slim controller, which logic separated into multiples Mediatr request handlers

Andy Blyth

Andy Blyth, a Technical Architect with a keen interest in martial arts, occasionally ventures into blogging when memory serves.

Andy Blyth