More object oriented development with Page Type Builder


Published on

An introduction to Page Type Builder, a plugin that introduces a more object oriented and easier way to work with EPiServer CMS.

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Three of the mostimportantcomponentswhendeveloping with EPiServer is Page Types, PageDataobjects and templates.A PageDataobject is sort of an instance of a PageType. A PageType is sort of like class, but not quite. Let’stake a look at whatimplications this has for oureveryday work.
  • Wecreate page types in EPiServer’sadmin mode.
  • Weaddproperties to page types, again in EPiServer’sadmin mode.
  • Weaddmarkup to pages and usercontrols, oftenusingwebcontrols that ship with EPiServer, to display the properties.
  • Whenweneedsomecustombehaviorweadd it to pages,customcontrols or some sort of utilityclasses.
  • Althoughweuse a staticallytypedlanguageweuse a lot of magic strings and therebybypass the checks performed by the compiler.It can be discussed, but I personallydon’tthink that determiningwhat to display as a heading for a page is the presentation layer’sjob, it’s the page’s.Since all pages are just instance of PageDatawehave to rely on filtering by numericalID’swhenwewant to find pages of a certain page type. This is extra problematicwhenwewant pages that may be of several different page typessincethere’s no inheritancebetween page types and theycan’timplement interfaces.
  • What if each page type was actually a class? And whatifeachPageDataobjectwas an instance of that class? And whatif the templateknewwhichtype, or basetype, it was rendering?If that was the casewewouldhaveinheritancebetween page types. Wecouldstrongly access propertyvalues. And wecouldeasily filter pages not only by typebutalso by whetherit’stypeimplements an interface or not.
  • Page TypeBuildersolves this (the previousslide).Page TypeBuilderdoesquite a lot of things, but the twomostimportantthings is that it creates and updates page typebased on classes that we, or third party products, candefine and that it interceptsrequests for PageDataobjects to DataFactory’smethods and replaces the objects that theyreturn with instances of the class that defines the page’s page type.
  • As wedefine page types in code all wehave to do to add or update page types, properties and tabs is writecode, compile and go to the site.As classes are used to define page typesweeffectively get inheritancebetween page types.SinceDataFactorynowreturnsinstances of ourclasseswe get stronglytyped access to propertyvalues.Another benefit of DataFactoryreturninginstances of ourclassesinstead of just PageData is that wecanplacelogic in theseclasses. I’llsoon show a couple of example of how this can work.Aseach page is an instance of a class that canhaveit’sownspecificbehaviour and alsoimplement interfaces wecanusepolymorphism. That is several page typescanhave the same method with the same signature that is implemented in different ways. Wecanthenusecode that is uses this methodbutdoesn’tknowaboutit’simplementation.Since Page TypeBuilderadds and updates page types, properties and tabs all wehave to dowhenwewantneed to do a release that involves page typechanges is to deployourassemblies.The moreobjectorientedway of working with Page TypeBuildercan a cornerstone in writingcode that it’spossible to writeunit tests for. Page TypeBuilderalso offers a moreconcreteadvantagewhen it comes to testing. Sincewecanmovesomelogic from the hard-to-test presentation layerintoour page typeclasses, and Page TypeBuilder has a feature for injectingdependenciesintoPageDataobjectswecanputquiteeasily test it.
  • I’veputsomeeffortintomaking it as easy as possible to get started with Page TypeBuilder. This meansthere’s no configuration or installationrequired. All you have to do is download it and put the assemblies that the Zipfilecontainsinto your bin folder and add a reference to PageTypeBuilder.dll.Of course, in a realistic scenario you wouldprobablywant to putthird party libraries like this somewhereelse and havethemautomaticallycopied to your bin folder when you compile. In that case you need to ensure that the two Castle assemblies are copied to the bin folder as Page TypeBuilder is dependent on them.PageTypeBuilderrequires EPiServer CMS 5 R2 SP2 or EPiServer CMS 6.
  • To create a page type all you have to do is add a new class in your project that inherits from TypedPageData and has a PageTypeattribute. Given thesefewlines of code Page TypeBuilderwillcreate a new page typenamedArticle as soon as you compile and browse the site.Normallywhenwecreate a page typewehave to specify a fewthingssuch as a path to the template that shouldrender the page type. Page TypeBuilderdoesn’t force us to do this and instead has a fewconventions. For instance, ifwedon’tspecify a path to the template it willautomatically be set to default.aspx.
  • By setting properties on the PageTypeattributewecanspecifyevery setting for a page type that wecanspecify in EPiServer’sadmin mode.By default the name of the page typewill be the class’ namebutwecanspecify that as wellusing the PageTypeproperty. Be awarethough that the name is used to identify the page type.
  • Tocreateproperties all wehave to do is add a codeproperty to a page typeclass and add a PageTypePropertyattribute to it.Again Page TypeBuilderwilluseit’s default conventions for mandatory settings. For instance, codeproperties of type string willautomatically be created as XHTML string properties.
  • Just as for page typeswecanuseproperties of the PageTypePropertyattribute to specifyeverything that wecanspecify in admin mode. However, it does not handle the new property settings that wasintroduced in EPiServer CMS 6. At least not yet.
  • Oncewehave a page typewewillprobablywant to render it using a template. Creating templateswhenusing Page TypeBuilder is verysimillar to how you do it when you aren’tusing Page TypeBuilder. The onlydifference, which is optional, is that you inherit from Page TypeBuilder’sgenericTemplatePage, or UserControllBase, instead of the EPiServer equivalents. These in turninherit from EPiServer’sclasses with the same name so no functionality is lost.
  • By inheriting from Page TypeBuildersgenericbaseclasses for pages or usercontrols the CurrentPagepropertywill be of the specifiedtype, enablingstronglytyped access to the currentpage’sproperties.
  • Previouslywesaw a verybasicimplementation of a propertyusingautomatic getters and setters. Wecanalsoimplementpropertiesusing an extension method for PageDatanamedGetPropertyValue. This waywecancreateproperties with logic in them. In this casewe check if the headingproperty is set and return it if it is. If it isn’twereturn the page’sname. This waywhenwewant to display a heading for the page wedon’thave to createanyconditionallogic, just display the page’sHeadingcodeproperty. This wayweencapsulate data where it belong and we get less code repetition.
  • AsDataFactorynowreturnsinstances of ourclasseswecan not just addlogic to properties. Wecanalsoaddregularmethods to our page types.This exampleillustratestwothings. First of all that wecanadd a methodsuch as GetComments to ourArticle page typeclass. Second that wecan filter pages by type.In a real scenarioweprobablywouldn’twant to callDataFactory.Instance from the methodbutinject an instance of an interface into the object.
  • More object oriented development with Page Type Builder

    1. 1. More object oriented development with Page Type Builder<br />-or how to have a little more fun at work<br />
    2. 2. Agenda<br />Problems with the standard EPiSevermodel<br />How Page TypeBuildersolves the problems<br />How to use Page TypeBuilder<br />Questions<br />
    3. 3. The EPiServer Model<br />PageType<br />Is, sort of, an <br />instance of<br />Decideswhich<br />template to use<br />PageData<br />Page (.ASPX)<br />Renderscontent from<br />
    4. 4. Deardiary, today I…<br />…created Page Types<br />
    5. 5. Deardiary, today I…<br />…addedproperties<br />
    6. 6. Deardiary, today I…<br />…addedmarkup to display properties<br />
    7. 7. Deardiary, today I…<br />…wrotesomecode<br />
    8. 8. Deardiary, today I…<br />…did a release<br />
    9. 9. A few problems<br />Wespend a lot of time not writingcode<br />Wedefine ”templates” for objects in a webbased UI<br />There’s no inheritancebetween page types<br />Synchronizing different environments is a pain<br />ObjectOrientation?<br />Weuse a lot of ”magic strings”<br />Weplacelogic in the presentation layer that doesn’tnecessarilybelongthere<br />WedistinguishPageDataobjects by the ID of their Page Type<br />
    10. 10. Whatif…<br />Article<br />Decideswhich<br />template to use<br />Actually is an instance of<br />ArticlePage<br />Page<Article> (.ASPX)<br />Renderscontent from,<br />knowing the type<br />
    11. 11. Page TypeBuilder<br />An opensourceproject<br />Creates and updates page typesbased on classes<br />Ensures that DataFactoryreturnsinstances of your classes<br />
    12. 12. By using Page TypeBuilderwe get<br />Less switchingbetween Visual Studio and Admin<br />Inheritancebetween page types<br />Stronglytyped access to properties<br />The ability to placelogic in pages<br />Polymorphism<br />Faster and easier releases<br />Bettertestability<br />
    13. 13. Getting started<br />Download from<br />ReferencePageTypeBuilder.dll<br />CopyCastle.Core.dll and Castle.DynamicProxy2.dll to your bin folder<br />
    14. 14. Creating a page type<br />usingPageTypeBuilder;<br />namespaceMySite.PageTypes<br />{<br />[PageType]<br />public classArticle : TypedPageData<br /> {<br /> }<br />}<br />
    15. 15. Specifying settings<br />[PageType(<br />Filename ="/Templates/Article.aspx”,<br />DefaultChildSortOrder =FilterSortOrder.Index)]<br />public classArticle : TypedPageData<br />{<br />}<br />
    16. 16. Addingproperties<br />[PageType]<br />public classArticle : TypedPageData<br />{<br /> [PageTypeProperty]<br />public virtual stringMainBody { get; set; }<br />}<br />
    17. 17. Specifyingproperty settings<br />[PageType]<br />public classArticle : TypedPageData<br />{<br /> [PageTypeProperty(<br />EditCaption ="Main body",<br />HelpText = "Will be shown in the main content area of the page",<br />Type = typeof(PropertyXhtmlString))]<br />public virtual stringMainBody { get; set; }<br />}<br />
    18. 18. Creating a template<br />usingPageTypeBuilder.UI;<br />namespaceMySite.Templates<br />{<br />public partialclassArticle : TemplatePage<PageTypes.Article><br /> {<br /> }<br />}<br />
    19. 19. Creating a template<br /><%@ PageLanguage="C#"CodeBehind="Article.aspx.cs"<br />Inherits="MySite.Templates.Article"<br />MasterPageFile="~/Templates/MasterPages/MasterPage.master"%><br /><asp:ContentContentPlaceHolderID="MainBodyRegion"runat="server"><br /><%= CurrentPage.MainBody%><br /></asp:Content><br />
    20. 20. Addlogic to properties<br />[PageTypeProperty(Type = typeof(PropertyString))]<br />public string Heading<br />{<br /> get<br />{<br /> string heading=<br />this.GetPropertyValue(page => page.Heading);<br /> if (!string.IsNullOrEmpty(heading))<br /> return heading;<br /> return PageName;<br />}<br />}<br />
    21. 21. Addingmethods to page types<br />[PageType]<br />public classArticle : TypedPageData<br />{<br /> publicIEnumerable<Comment> GetComments()<br /> {<br />return<br />DataFactory.Instance.GetChildren(PageLink)<br /> .OfType<Comment>();<br /> }<br />}<br />
    22. 22. DependencyInjection<br />[PageType]<br />public classArticle : TypedPageData<br />{<br />privateIPageSourcedataFactory;<br />public Article(IPageSourcedataFactory)<br />{<br />this.dataFactory = dataFactory;<br />}<br /> publicIEnumerable<Comment> GetComments()<br /> {<br />returndataFactory.GetChildren(PageLink)<br /> .OfType<Comment>();<br /> }<br />}<br />
    23. 23. Where to learnmore<br /><br /><ul><li>Links to tutorials
    24. 24. Links to 40+ blog posts</li></li></ul><li>Thank you!<br />Questions?<br /><br />@joelabrahamsson<br /><br />Photographs from <br /> <br />and <br /><br />