|Number of votes:||6|
When looking at how developers using EPiServer 7 wrote their typed models we noticed a frequent use of the "BackingType" attribute. For developers used to EPiServer CMS 5 and 6 this might seem quite natural since everyone was forced to work with classes inheriting from PropertyData. Since one goal of EPiServer 7 has been to hide the internal property classes as much as possible and focus on the value types instead. We would really like to make it possible to define your models without the BackingType attribute for most of the use cases. If we take a look of what the property data classes have been responsible for it's mainly three things:
With the changes done to page-(content) editing in EPiServer 7 number 2.2 is no longer valid unless you use the legacy edit view.
In the latest CTP build you can give hint's on which editor should be used as described in my previous blog post:
After that we have done some more changes to be able to define your model without having to care about the backing property type. The first is that the most common types have their own value types, for instance XhtmlString, XForm and Url. If we look at how the model could look like it would be something like this:
And when editing in forms mode it will look like this:
But what if you would like to add an URL to an image? Creating a property with the type Url and defining an EditorHint("ImageUrl") attribute would make the Url be edited as an Image but the output on the page would still be a link to an URL instead of displaying an image. So in this case you would still have had to define a BackingType(typeof(PropertyUrl)) to select the correct Property Control.
To remove this need we have decided to use the UIHint attribute instead (from the System.ComponentModel.DataAnnotations namespace) and added support to select renderer depending on either this attribute or the Tag setting on a EPiServer Property web control, the same way as you can select a specific renderer for blocks. In this case the model would look like this:
Note: UIHint takes a string as a parameter but in this case we are using the UIHint class in EPiServer.Web that defines known hints as constants.
What happens behind the scenes in this case is that the property is backed by a PropertyUrl instance but a PropertyImageUrlControl will be used for rendering since it is declared as a renderer for the tag "Image" that matches the UI hint.
If you want to define a hint only selecting editor or renderer this is possible by defining the presentation layer property for the UIHint attribute. For instance:
Note: PresentationLayer also takes a string but there is a class with constants for known presentaion layers in EPiServer.Framework.DataAnnotations.
In this example EPiServer will use the default editor for selecting images but search for a registered template with the tag "CustomImage".
The BackingType attribute still works but should only be used where you have a custom property that actually does something with the data, not only assigns a custom property control. To make sure that your custom property controls can be assigned without having to have a custom property you will have to add a TemplateDescriptor attribute to the custom property control:
In current builds of EPiServer 7 we have used PropertyString as the backing type for the value type string. Since property string has a built in limitation of storing 255 characters you had to specify PropertyLongString as the backing type if you wanted to store strings longer than 255 characters. Since data storage of potentially long strings really is not an issue in the currently supported versions of MS SQL Server we have changed this to default to PropertyLongString which means that a string property has no length limitation by default. If you want to limit the length of strings you can use the StringLength attribute to validate the length.
We also changed the default editor for PropertyLongString to be a simple input instead of a textarea. If you want to edit the string using a textarea you can simply define a UIHint(UIHint.Textarea) to your string property.
The changes done to support the features above have some side effects when upgrading a site that has templates that works without typed models that you might want to be aware of:
If you are upgrading a site with weakly typed page types and are using properties of type PropertyLongString you will get standard inputs instead of text areas. You can set the OperationCompatibility setting on your site settings element to include the flag LongStringLegacyControl to ensure that you still get text areas when editing your long string properties.
Some of the changes to be able to implement this was to make sure that our property implementations returned an object of the type that said they returned. A properties return type is defined in the "PropertyValueType"-property. For some types, the internal storage type was returned instead of the defined type. For instance, PropertyXhtmlString, PropertyUrl and PropertyXForm all returned strings. Now these will return the value types XHtmlString, Url or Xform instead. For typed code you will probably not access data from the property collection directly in most cases (dynamic properties is a example where you still have to access data untyped) but for legacy code this might be a problem. For instance:
var value = (string)CurrentPage[“Mainbody”];
The code above would throw an exception in EPiServer 7 if the mainbody property was defined as an XhtmlString since the return value of the property is now an XhtmlString and not a string. So how do you deal with this? There are two ways:
1. Set the OperationCompabilityOption setting to include the flag LegacyPropertyValueType and these properties will still return strings in EPiServer 7. This can only be used if you are not using any typed models on your site.
2. Refactor the code where you are accessing the data untyped and casting it to a string. My colleague Magnus Rahl will create a separate post in this topic.