Reinoud van Dalen

February 16, 2014

InferType for Glass Umbraco

The Glass.Mapper framework for Umbraco is premature compared to Sitecore and since this awesome project is opensource I can contribute wherever is needed. First thing I really want: InferType.

This feature allows you to request a base level template and actually get the most specific type available for that document. So I have this BaseCommon class which I apply to all my Umbraco models. It has a Children property of type IEnumerable<BaseCommon> and when I access this property I want to be able to filter them on a specific type.

Glass.Mapper supports this behaviour with InferType, but it looks like it is not enabled for Umbraco. So you can specify all you want, but it will have no effect.

In order to hook in to the type resolver we need to make an InferTypeResolver much like the one for Sitecore:

Some description 

With this in place, we need to register the resolver in the UmbracoInstaller from Glass.Mapper.Umb.CastleWindsor and voila! We can access the children property and check if there are any specific types available.

Some description

You can check out the source code in my fork. I have made a pull request so the resolver can be added in the original project.

ps. Don't forget to specify your DocumentTypeAlias in the UmbracoType attribute.

TAGS: umbraco glass