Monday 17 October 2011

Getting Umbraco radiobutton list values and prevalues


You know how umbraco gives you an int when you're using a radiobutton list. Don't you wish it would give you the value selected in c#... Even possibly the values which are defined so you can check against them?

I discovered a (possibly convoluted) way of getting these. Note: the node object in the example below is of type Document

// get the key values defined in the datatype for property "myProperty"
var keyValues = ((KeyValuePrevalueEditor)node.getProperty("myProperty").PropertyType.DataTypeDefinition.DataType.PrevalueEditor.Editor).PrevaluesAsKeyValuePairList;

// get the value which maps to the int which Umbraco saves when you select the radiobutton
var value = umbraco.library.GetPreValueAsString(int.Parse(node.getProperty("myProperty").Value.ToString()))

Saturday 8 October 2011

Umbraco Model and DynamicNode overhead part 2


The results from the previous post were from a page which had a few macros on it. In true scientific fashion I decided to isolate the issue. I downloaded a fresh copy of Umbraco from the web matrix and ran it on iis express.

I made a doctype with nothing except a title. I made 2 templates and 2 macroscripts. One using Model, the other using DynamicNode.


Here are the macroscripts:

Model macroscript
@{
@Model.title
}

DynamicNode macroscript
@using umbraco.MacroEngines
@{
DynamicNode d = new DynamicNode(Model.Id);
@d.GetProperty("title");
}


The 2 following images are example traces (Model followed by DynamicNode). I am interested in the time it takes for the macro to finish loading. Ie. the last line Loading IMacroEngine script [done]

Model




Dynamic Node




Here is a table of results showing the Loading IMacroEngine script [done] line:


Dynamic node loaded on average 0.000041646741092 seconds faster than Model.

This does not seem like much, but it is interesting that instantiating a DynamicNode and getting a value from it is quicker than getting a value from Model. I was told that Model is dynamic but is actually a DynamicNode also. It sort of makes sense that a dynamic object would be more intensive than a typed one (even of the same type).

I'm wondering if there is some sort of effect of having multiple scripts (or nested scripts, or even possibly nested masterpages) on the page which compounds the time taken to load a single macro, and hence causing the script to load slower in the previous post.

I would be interested if people had some input.



UPDATE...
I decided to run the tests again except checking if the value is null.

Quick results...
Average (Model)
0.005558425393959
Average (DynamicNode)
0.00527851060727


Difference
0.000279914786689


So the difference is larger when you start doing things with Model.


Friday 7 October 2011

Umbraco Model and DynamicNode overhead





Just a quick post to show something interesting I discovered with Umbraco 4.7.1.


These 3 if statements do the same thing. However the first 2 turn out to be 2-3 times more expensive in terms of execution time.


if (Model.title == null)
{}





if (Model.title is umbraco.MacroEngines.DynamicNull)
{}





var current = new DynamicNode(Model.Id);
if (current.GetProperty("title") != null)
{}




What's more, is that if you call AnscestorOrSelf() on Model vs current (my instantiated DynamicNode) there is a doubling of those results.

It seems that there is some extra overhead going on with Model.