Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Mikael Nordberg
May 11, 2009
  4835
(0 votes)

Strongly typed property access- Part two

As promised by Daniel in part one, here is part two!

In this post I’ll try to explain how we could automatically deploy our mapped properties that we use to get strongly typed properties (see Daniels post) in our templates by using some reflection, conventions and attributes.

Creating a new Template

First we create a new Web Form/Page Template in Visual Studio and change the inheritance from EPiServer.TemplatePage to our own extended class TemplatePage<T>. 

To add properties we create a class-file for the properties that we will use in our template. Neither the PageType or associated properties has to be present in EPiServer.

   1: public class MyPageProperties : PropertiesBase
   2: {        
   3:     public PropertyString Heading { get; set; }
   4:     public PropertyXhtmlString MainBody { get; set; }        
   5:     public PropertyUrl SomeUrl { get; set; }
   6: }

PageTypeBuilder

PageTypeBuilder is the tool responsible for creating and updating  PageTypes and properties in EPiServer.

To execute it, we need to add some code when the application starts, for instance in a PlugIn or in Global.asax.

   1: public class PageTypeDefPlugin : EPiServer.PlugIn.PlugInAttribute
   2: {
   3:     public static void Start()
   4:     {
   5:         var builder = new PageTypeBuilder<PropertiesBase>(Assembly.GetExecutingAssembly().FullName)
   6:                           {
   7:                                   Settings = new PageTypeBuilderSettings {TabName = "Information", VirtualPathRoot = @"\Pages\"}
   8:                           };
   9:  
  10:         builder.UpdatePageTypes();
  11:     }
  12: }

PropertiesBase is added as a generic type and the BuildPageTypes method takes a params string[] with names of assemblies to scan for mapping classes.

The tool will look for classes that are subclasses of T (= PropertiesBase) and build PageTypes out of these. Each class will become a PageType and each property will be added as a  EPiServer property.

For global default settings a PageTypeBuilderSettings object is used. Here it’s possible to add virtual path, default tab, if unmapped properties should be deleted  or not and if existing properties should be updated.

Attributes

But what about Help texts,  Tabs and Descriptions? The tool will guess/use conventions for these. The class name - “Properties” will be the name for the PageType (class “StartProperties” = PageType “Start”), if no Tab name is specified, the property will be added to the “Information” tab (if not overridden by the global settings) and so forth.

To override this and to add additional information we could decorate our mapping classes with attributes.  Attributes could be Name of the page/property, Edit Heading, Tab, Default value, Language specific and some others. Pretty much the same as we have in Admin mode.

If we have properties that are shared between templates these could be added to either the PropertiesBase class or to another class that inherits PropertiesBase and then use this class as subclass for our mappings.  Since we don’t want this class to be a PageType there is a [Ignore] attribute that could come in handy.

The sort order of the properties will be the same as the order in our mapping classes.

Screenshots

some pictures might say more  than 1000 words…

 1.pagetypes
Before

For mapping class we use this:

   1: public class MyPageProperties : PropertiesBase
   2: {
   3:     [HelpText("Ingress")]        
   4:     public PropertyString MainIntro { get; set; }
   5:     [HelpText("Primär text")]        
   6:     public PropertyXhtmlString MainBody { get; set; }
   7:     [HelpText("Länk till bild")]        
   8:     public PropertyImageUrl ImageUrl { get; set; }
   9: }

This will result in this new PageType:  
2.mypage   Sort order of properties will be the same as in the mapping class

Code is king!

Since we add, update and delete properties when the application starts we will not depend on a specific database. This could be useful in a continuous integration scenario where the development database differs from the database used for test and of course if all developers in a team have their own (local) database.

Summary

This tool is pretty much a “proof of concept” that could be used to complement our way to handle strongly typed properties. It' can not (yet) handle all features of PageTypes that exists in EPiServer (dynamic properties to mention one) but will ease the pain of creating PageTypes and  adding properties ;)

So, what do you think?

May 11, 2009

Comments

Please login to comment.
Latest blogs
Level Up with Optimizely's Newly Relaunched Certifications!

We're thrilled to announce the relaunch of our Optimizely Certifications—designed to help partners, customers, and developers redefine what it mean...

Satata Satez | Jan 14, 2025

Introducing AI Assistance for DBLocalizationProvider

The LocalizationProvider for Optimizely has long been a powerful tool for enhancing the localization capabilities of Optimizely CMS. Designed to ma...

Luc Gosso (MVP) | Jan 14, 2025 | Syndicated blog

Order tabs with drag and drop - Blazor

I have started to play around a little with Blazor and the best way to learn is to reimplement some old stuff for CMS12. So I took a look at my old...

Per Nergård | Jan 14, 2025

Product Recommendations - Common Pitfalls

With the added freedom and flexibility that the release of the self-service widgets feature for Product Recommendations provides you as...

Dylan Walker | Jan 14, 2025

My blog is now running using Optimizely CMS!

It's official! You are currently reading this post on my shiny new Optimizely CMS website.  In the past weeks, I have been quite busy crunching eve...

David Drouin-Prince | Jan 12, 2025 | Syndicated blog

Developer meetup - Manchester, 23rd January

Yes, it's that time of year again where tradition dictates that people reflect on the year gone by and brace themselves for the year ahead, and wha...

Paul Gruffydd | Jan 9, 2025