Skip to content

Instantly share code, notes, and snippets.

@jbreuer
Created November 20, 2014 10:51
Show Gist options
  • 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();
}
@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