Episerver - Why BlockData Doesn't Implement IContent

Does anyone know why the BlockData class does not directly implement IContent? I know that during the extraction of BlockData from the database, the proxy server created by Castle implements IContent.

If StackOverflow is not suitable for such a question, move it.

+7
episerver episerver-7
source share
3 answers

Johan Bjรถrnfot on EPiServer explains some of the details in this post .

Excerpts:

โ€œIn previous versions of CMS, there were pages (PageData) - the only type of content that processed the content repository (traditionally DataFactory). In CMS7, this has changed, so now the content repository (IContentRepository) processes instances of IContent. This means the requirement is a .NET type that can be saved / loaded from the content repository, is that it implements the EPiServer.Core.IContent interface.

There are some IContent implementations built into the CMS, such as PageData and ContentFolder (used to group instances of data blocks), as well as the ability to register custom IContent implementations. If you look at BlockData, although you notice that it does not implement IContent, how are shared block instances handled?

The answer is that at runtime, when an instance of a common block is created (for example, by calling IContentRepository.GetDefault, where T is a type inheriting from BlockData), CMS will create a new .NET type that inherits T using a technique called mixin where the newly created subclass will implement some additional interfaces (including IContent).

+5
source share

BlockData implements IContent, because it is designed to work both when adding content to another element, for example, a PageData instance (local aka block), or as a separate instance (akaShared block). In the latter case, the interface is added using a combination, although Castle Windsor, so that it can be referenced.

The solution for this design was based on being able to use the same rendering templates, regardless of whether the block is local or shared. Therefore, the choice was between having a large number of empty properties on local blocks or the current solution using mixins. Both options were tested, and mixins were chosen as the preferred solution, although it was not ideal.

+4
source share

BlockData "implements IContent", just do:

var myContent = (IContent)myBlock; 

But, if you accidentally control a block that itself has a property (not a ContentReference), this throw throws an exception.

This will be true for 100% of all cases (... using Math.Round).

+1
source share

All Articles