In a previous post, I was describing how certain Component link fields in a Schema can map to more than just one type of domain model. At the same time, a Multimedia link field in a Schema can allow more than just one Multimedia Schema as its linked Multimedia Component.
In the example below, property RelatedItems could link to either one of models Article, Internal Link or External Link.
The same occurs with Multimedia link BinaryAsset, which can link to any of the multimedia class types PDF Document, Excel Document or Work Document.
These links are what I called 'umbrella models', which can hold several types of domain models, but for which at compile time, we cannot tell actual type it holds. Therefore, we must use a high level general type like BaseModel and make the differentiation only at run-time, using code like this:
We will see in a next post how we are building these models, and more interestingly, how we build these umbrella models.
In the example below, property RelatedItems could link to either one of models Article, Internal Link or External Link.
public class Device : BaseModel
{
///
<summary>
///
Could be a heterogenous collection of: Article, Internal Link, External
Link
///
</summary>
public IList<BaseModel> RelatedItems { get; set; }
public class Software
{
///
<summary>
///
Could be one of PDF Document, Excel Document, Word
Document
///
</summary>
public BaseModel BinaryAsset { get; set; }
public static MvcHtmlString Link(this HtmlHelper htmlHelper, BaseModel model)
{
if
(model is InternalLink)
{
// Magic happens here
}
else
if (model is ExternalLink)
{
// Magic happens here
}
Comments