EPiServer CMS 7: BackingTypeResolver

Or why it can be important to override PropertyValueType on your custom properties.

When you create a Page Type in code in EPiServer CMS 7 you create a new class inheriting from PageData and add virtual properties for all the fields you want on you page. During initialization EPiServer CMS will scan all assemblies, locate these classes and create Page Types and Property Definitions that you can view (and modify) in EPiServers admin mode.

Each property in code will be mapped to an underlying property definition type (a subclass of PropertyData) that is responsible for how the value is persisted to the database (among many things).

Understanding the BackingTypeResolver

Mapping between the types that you use in your code and what PropertyData subclass should be used is handled by BackingTypeResolver class.

First it has a set of hard coded rules. This is the rules used in the first release of EPiServer CMS 7:

Type Backing type used
Boolean (bool) PropertyBoolean
Byte (byte) PropertyNumber
DateTime PropertyDate
Decimal PropertyFloatNumber
Double PropertyFloatNumber
Float PropertyFloatNumber
Int16 PropertyNumber
Int32 (int) PropertyNumber
Int64 PropertyNumber
PageType PropertyPageType
Single PropertyFloatNumber
String (string) PropertyLongString
TimeSpan PropertyTimeSpan
Url PropertyUrl
XForm PropertyXForm
XhtmlString PropertyXhtmlString

 

If the type you used on your property is not in the table above the BackingTypeResolver will scan all registered property definition types and choose the first where your type is equal to the type returned by PropertyValueType. This will make it possible to use the following types on properties in addition to the ones above.

Type Backing type used
PageReference PropertyPageReference
CategoryList PropertyCategory
ContentReference PropertyContentReference
ContentArea PropertyContentArea
LinkItemCollection PropertyLinkCollection

Conclusion

The need to create your own custom properties in EPiServer CMS 7 is certainly lower than in earlier version since we use the UIHint attribute to control how the property value is edited and rendered in view mode.

If you find yourself decorating properties with the BackingType attribute to get them to use one of you Custom Properties consider overriding PropertyValueType.

Read more

Typed models in EPiServer 7 by Linus and How to define properties in EPiServer 7 – A quick reference by Alexander.

.