Vegard Solheim
May 18, 2020
  3608
(4 votes)

Customized Transformation of IContent for ContentDeliveryApi

I've created a convenient pattern to more easily customize and add additional data to the ContentApiModels delivered by ContentDeliveryApi. There are apparently some questions flying around about how to best use it, and I'll be glad to post my idea on one way to do it.

Why customize it?

The ContentDeliveryApi does much of the magic in the background (which is good), but it's not entirely obvious how to extend it in certain ways. Episerver's own Music Festival SPA demo gives a clue on how to do it with the ExtendedContentModelMapper which intercepts the transformation, but that demo only adds an URL and flattens the properties for all pages.

I've often needed to add some data that should be for all pages, and some which should be specific to some pagetypes. Essentially like a controller for ContentApi. PropertyConverters does this for properties on pages, but there was no obvious way to do the same for pages and blocks.

What does it do?

The pattern, for lack of a better word, extends the ExtendedContentModelMapper from Music Festival with the ability to add custom model converters for all types of IContent, i.e. pagetypes, mediadata, and blocks - the exception being for blocks as properties, which are handled by propertyconverters.

  1. During startup it scans the assembly for any classes implementing a specific interface.
  2. When a page is requested through ContentDeliveryApi it enters the ExtendedContentModelMapper, which finds the correct converter for that IContent.
  3. The converter uses the default IContentModelMapper for initial transformation, which makes this pattern essentially maintenance-free, then you can add any additional data to the ContentApiModel.
  4. Afterwards the resulting ContentApiModel is sent to the client as usual.

It supports an optional hierarchy so that e.g. a BasePageConverter can provide language links which all pages should have, then the specific pagetype can add additional data like user-specific information.

Being able to have all this in one spot makes it easier to handle, works essentially like a controller so that devs can follow-ish the MVC-pattern, and alleviates the need for extra web API-calls for page-specific information. Since it finds the converters through assembly scanning there's no need to register them anywhere.

Update! Dependency injection is now supported

I have now added dependency injection to the converter-classes. It uses StructureMap as the IoC tool. The code is on the master branch.

If you prefer to not have dependency injection, or plan to use another tool, the basic code is in the branch "Basic_without_dependency_injection".

Here's the code.

https://github.com/VegardSolheim/Demo-ModelConverters-Epi-ContentApi

May 18, 2020

Comments

Jørgen Tonvang
Jørgen Tonvang May 19, 2020 10:08 AM

Once you start implementing more complex Episerver solutions/SPAs based on Episerver, this is super useful. Great work!

Please login to comment.
Latest blogs
Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog

ExcludeDeleted(): Prevent Trashed Content from Appearing in Search Results

Introduction In Optimizely CMS, content that is moved to the trash can still appear in search results if it’s not explicitly excluded using the...

Ashish Rasal | Nov 7, 2024

CMS + CMP + Graph integration

We have just released a new package https://nuget.optimizely.com/package/?id=EPiServer.Cms.WelcomeIntegration.Graph which changes the way CMS fetch...

Bartosz Sekula | Nov 5, 2024

Block type selection doesn't work

Imagine you're trying to create a new block in a specific content area. You click the "Create" link, expecting to see a CMS modal with a list of...

Damian Smutek | Nov 4, 2024 | Syndicated blog

.NET 8 FAQ

I have previously written about .NET compatibility in general and .NET 8 in particular, see blog posts here , here and here . With the end of suppo...

Magnus Rahl | Nov 4, 2024

Dynamic packages in Commerce Connect

In Optimizely Commerce Connect, you can group different items using packages and bundles. Package: A package has one or more versions of a product...

K Khan | Nov 1, 2024