"Is using UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current) in a static method (called from web) OK?"
When I say "called from web" I'm refering to calling it from within Umbraco (ie. from a view or maybe a UserControl). I do NOT mean calling it form outside of Umbraco, where obviously the UmbracoContext wouldn't exist.
The main reason I find myself needing access to UmbracoHelper is to create new TypedContent based on a node id.
To provide a concrete example, here is an extension method I created to get back IPublishedContent items for a multi-node tree picker (set to return CSV):
public static List<IPublishedContent> GetContentFromMultiNodePicker(this IPublishedContent content, string alias, bool recursive)
{
List<IPublishedContent> list = new List<IPublishedContent>();
string data = content.GetPropertyValue<string>(alias, recursive, null);
if (!String.IsNullOrEmpty(data))
{
var ids = data.Split(',');
UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
foreach (string id in ids)
{
var node = helper.TypedContent(id);
if (node != null)
{
list.Add(node);
}
}
}
return list;
}
The only way I can find to get back actual nodes (IPublishedContent) is via the TypedContent method of the UmbracoHelper instance.
I find there are many other places where I similary need to do this - mainly when I have a library that accepts a node by Id (as an int) but needs to convert it into IPublishedContent. I want to abstract common code like this into reusable methods.
Currently this works fine - I've had no issues. But I'm aware that Shannon seems to think this is a bad idea, which worries me :) So, what is the verdict?
Cheers for info. I did intially pass in the UmbracoHelper instance to the method, but it seemed a bit "clumsy". Often a lot of the methods I write are consumed by front-end guys so I try and make the method signatures are simple as possible. As instantiating the UmbracoHelper in the method worked I thought I might as well remove one (seemingly) uneeded parameter.
I thought someone might mention property editor converters :) Yeah, I realise they are the best way of achieveing what I was doing, but I was more interested in the wider point of whether it was wrong or not to create a helper instance within a method.
I was looking into this the other day... there was a very long thread on the subject. Maybe things have moved on since then, but at the time Shannon was commenting that the new UmbracoHelper(UmbracoContext.Current) is the way to go (see page 5).
Instantiating UmbracoHelper in Static Class Library
This question arises out of a question I asked on Twitter:
When I say "called from web" I'm refering to calling it from within Umbraco (ie. from a view or maybe a UserControl). I do NOT mean calling it form outside of Umbraco, where obviously the UmbracoContext wouldn't exist.
The main reason I find myself needing access to UmbracoHelper is to create new TypedContent based on a node id.
To provide a concrete example, here is an extension method I created to get back IPublishedContent items for a multi-node tree picker (set to return CSV):
Hi Dan
What I usually do here, is pass the current UmbracoContext into the method to use the 'typedness'
but that's if I'm creating a razor helper in app_code
eg:
@helper DisplayImage(IPublishedContent content, UmbracoHelper umbracoHelper, string mobileImageAlias, string largeImageAlias)
and then I use it in the method;
umbracoHelper.TypedContentAtPath("//thing")....
If the project is being built with visual studio, then I often create further extension methods onto UmbracoHelper for my custom methods...
eg
public static class MyUmbracoHelpers
{
public static IEnumerable<IPublishedContent> GetAllThings(this UmbracoHelper umbraco, string someParameter){
// logic to get and return things
}
}
then you can just call it as @Umbraco.GetAllThings("parameterthing")
but in your example above you probably want to look at a PropertyEditorValueConverter,
there is one for the multinode tree picker in this package;
http://our.umbraco.org/projects/developer-tools/umbraco-core-property-editor-converters
With the MultinodeTreePickerPropertyEditorValueConverter in your project, instead of using your helper above you'd be able to write
var allPickedNodes = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("myPickerPropertyAlias")
if that makes sense ?
Hi Marc,
Cheers for info. I did intially pass in the UmbracoHelper instance to the method, but it seemed a bit "clumsy". Often a lot of the methods I write are consumed by front-end guys so I try and make the method signatures are simple as possible. As instantiating the UmbracoHelper in the method worked I thought I might as well remove one (seemingly) uneeded parameter.
I thought someone might mention property editor converters :) Yeah, I realise they are the best way of achieveing what I was doing, but I was more interested in the wider point of whether it was wrong or not to create a helper instance within a method.
Would love to here the views of the core team?
If the frontend people don't need to edit the logic of the function,
then extension methods to the UmbracoHelper class, look most neat / magical :-)
Making it an extension of the UmbracoHelper class is a really good idea - I like it. I'm going to do that :)
Interestingly I looked at the source of the MultiNodeTreePickerPropertyEditorValueConverter control and it had this in it...
So...!
I was looking into this the other day... there was a very long thread on the subject. Maybe things have moved on since then, but at the time Shannon was commenting that the new UmbracoHelper(UmbracoContext.Current) is the way to go (see page 5).
is working on a reply...