Skip to content

Instantly share code, notes, and snippets.

@jbreuer
Created November 20, 2014 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbreuer/a396f1e4504e181e1c8a to your computer and use it in GitHub Desktop.
Save jbreuer/a396f1e4504e181e1c8a to your computer and use it in GitHub Desktop.
Strongly typed querying in Umbraco
/// <summary>
/// Return the all the data required for filtering and displaying object types.
/// </summary>
/// <returns></returns>
public static IEnumerable<ObjectTypeItem> GetObjectTypeItems()
{
//Get the node where all projects and object types are below.
var projectOverview = Umbraco.TypedContent(ConfigurationManager.AppSettings["projectOverviewId"]);
return
(
from project in projectOverview.Children<ProjectDetails>()
from objectType in project.Children<ObjectTypeDetails>()
let city = project.City
select new ObjectTypeItem()
{
ObjectType = objectType.DocumentTypeAlias.EnumParse<ObjectType>(true),
ProjectStatus = project.Status,
ObjectTypeTitle = objectType.Title,
ProjectTitle = project.Title,
CityTitle = city.Title,
FromPrice = objectType.FromPrice,
FromPriceExtra = objectType.FromPriceExtra,
TillPrice = objectType.TillPrice,
TillPriceExtra = objectType.TillPriceExtra,
NumberAvailable = objectType.NumberAvailable,
Location = project.Location,
Images = objectType.HeaderVisuals,
Url = objectType.Url
}
).ToList();
}
@JimBobSquarePants
Copy link

It's great isn't it? So much cleaner IMO.

That EnumParse<T> method looks interesting. I'd like to see the code behind that.

I've been using a custom tool based heavily on Lee Kelleher's Ditto but with a lot of performance improvements and enhancements. I can get it to automatically parse classes like this.

public class Carousel : WidgetBase
{
    /// <summary>
    /// Initializes a new instance of the <see cref="Carousel"/> class.
    /// </summary>
    /// <param name="content">
    /// The <see cref="IPublishedContent"/> representing the cached, published content.
    /// </param>
    public Carousel(IPublishedContent content)
        : base(content)
    {
        this.Panes = Enumerable.Empty<CarouselPane>();
    }

    /// <summary>
    /// Gets or sets the panes.
    /// </summary>
    public IEnumerable<CarouselPane> Panes { get; set; }

    /// <summary>
    /// Builds the widget view model.
    /// </summary>
    /// <param name="umbracoHelper">
    /// The umbraco Helper.
    /// </param>
    /// <returns>
    /// The <see cref="IRenderWidget"/>.
    /// </returns>
    public override IRenderWidget BuildViewModel(UmbracoHelper umbracoHelper)
    {
        RenderCarousel viewModel = new RenderCarousel(this);

        return viewModel;
    }
}

Note the CarouselPane type in the enumerable. It's able to recognise a typeconverter attribute attached to the type parameter and tell it what to do.

I'm slowly but surely getting Lee to accept pull requests to add some of the improvements to Ditto so we can all use it.

@jbreuer
Copy link
Author

jbreuer commented Jan 15, 2015

The EnumParse method is an extension method in the Umbraco source. You can use it if you add the Umbraco.Core namespace.

@richardterris
Copy link

Yep, ditto is what we're using too - just makes sense.

@Shazwazza
Copy link

Soooooo..... this is a static method. In this static method you are accessing an UmbracoHelper, therefore this means that your UmracoHelper is also static = very bad! :)

I wish everyone would please stop making static anythings unless it really makes sense. UmbracoHelper is a REQUEST based object because it relies on an UmbracoContext which is also a REQUEST based object. Anything static instantly has a SINGLETON/APPLICATION life span and thus never gets disposed.

@sotirisf
Copy link

@Shazwazza What if it was instantiated inside the static method? I mean, I use static methods all the time but I'm doing something like this:

var h = ContentHelper.GetHelper();

And this one is a method that @Nicholas-Westby was so kind to share with me - you can find it here: https://github.com/rhythmagency/rhythm.umbraco.extensions/blob/56a328f8c84d4f155d7201494be62ecfb0aaf32e/trunk/Rhythm.Extensions/Rhythm.Extensions/Helpers/ContentHelper.cs#L27 - it ensures that only one UmbracoHelper gets instantiated per HTTP request. But, even without that, why ditch statics all together and not just instantiate an UmbracoHelper?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment