Episerver - Why BlockData doesn't implement IContent

心不动则不痛 提交于 2019-12-22 04:46:20

问题


Does anybody knows why BlockData class doesn't directly implement IContent? I know that during BlockData is being retrieve from database, proxy created by Castle implements IContent.

If StackOverflow isn't suitable place for this kind of a question, please move it.


回答1:


Johan Björnfot at EPiServer explains some of the details in this post.

Excerpt:

"In previous versions of CMS was pages (PageData) the only content type that the content repository (traditionally DataFactory) handled. In CMS7 this has changed so now content repository (IContentRepository) handles IContent instances. This means that the requirement for a .NET type to be possible to save/load from content repository is that it implements the interface EPiServer.Core.IContent.

There are some implementations of IContent built into CMS like PageData and ContentFolder (used to group shared block instances) and it is also possible to register custom IContent implementations.If you look at BlockData though you will notice that it doesn’t implement IContent, how is then shared block instances handled?

The answer is that during runtime when a shared block instance is created (e.g. through a call to IContentRepository.GetDefault where T is a type inheriting from BlockData) the CMS will create a new .NET type inheriting T using a technic called mixin where the new generated subclass will implement some extra interfaces (including IContent)."




回答2:


BlockData does implement IContent as it is intended to work both when added to another content item such as a PageData instance (a.k.a. Local Block), and as a standalone instance (a.k.a.Shared Block). In latter case the interface is added by using a mix-in though Castle Windsor so that it can be referenced.

The decision for this construct was based on wanting to be able to use the same rendering templates regardless if a block is local or shared. Therefor the choice stood between having a large number of empty properties on local blocks or the current solution using mixins. Both options were tested and mixins was selected as the preferred solution even though it's not a perfect one.




回答3:


BlockData "does implement IContent", just do:

var myContent = (IContent)myBlock;

But, if you're by any chance handling a Block which itself is a property (not a ContentReference), that cast will throw an exception.

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



来源:https://stackoverflow.com/questions/29944431/episerver-why-blockdata-doesnt-implement-icontent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!