Using Interfaces to get more compatible content elements in Episerver

Interfaces are more or less basic stuff in programming and in Episerver development. You probably have seen these guys (IThisAndThat, IEtc..) all over the reference solutions (AlloyDemoKit, Quicksilver, …). In this blogpost, I’ll be taking a concrete approach how to make one of the AlloyDemoKit’s content blocks more compatible with other content elements using an Interface.

So, first things first. What is an Interface? If you already know the answer skip this paragraph, if not, have a look at one of the Stack Overflow’s answer. There are also Zombies in there: What is the definition of interface in object oriented programming?

Interfacing the Slide Show Block

In AlloyDemoKit we have a Slide Show Block (CarouselBlock) which acts as a container for carousel items (CarouselItemBlock). You can drag and drop those items to it, and everything works.

What if you want to drag a page to it, or an image? At this point it isn’t possible since the CarouselBlock’s content area accepts only specific carousel items. If we want to add pages to to it, we could simply add SitePageData as a AllowedContentType. However, the current code is expecting a predefined content properties and a CarouselItemBlock type. So that wouldn’t work.

The better and more robust way is to create a new Interface: ICarouselContent.

What you see here is an interface extracted from the CarouselItemBlock class. There is also a UIDescriptor which is required to get the AllowedContentType attribute work (have a look at the Episerver documentation). We also want to add a reference to the interface in the CarouselItemBlock.cs.

Next, we are going to make few changes to CarouselBlockController.cs, CarouselBlock.cs, CarouselViewModel.cs and CarouselItemBlock.cshtml

What I did was, I simply replaced the references of the CarouselItemBlock to our newly created ICarouselContent Interface in all of the files.

At this point, everything should be in place. We have kept the original functionality, but we have created a new extension point. Now we can tell any content element that it should implement ICarouselContent interface.

By doing that and implementing required content properties, you can drag and drop the element to the CarouselBlock’s contentarea. Here is an example implementation for the SitePageData.

You can now drag any page to the carousel that inherits SitePageData. If you want to drag images (or any other element), simply reference the ICarouselContent and implement it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s