Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glass Mapper: InferType is ignored when querying the SitecoreContext

I have installed the package Glass.Mapper.Sc.CastleWindsor in version 3.1.2.11 on my Sitecore 7.1 solution and am trying to work with inferred types. I have the following classes:

[SitecoreType]
public class ServiceConfiguration
{
    [SitecoreField(FieldName = "Service Id")]
    public virtual string ServiceId { get; set; }
}

[SitecoreType(TemplateId = "{26512C19-8D30-4A1E-A2CD-3BA89AF70E71}")]
public class JavascriptServiceConfiguration : ServiceConfiguration
{
    [SitecoreField(FieldName = "Is Header Responsive")]
    public virtual bool IsHeaderResponsive { get; set; }
}

And i have this item:

enter image description here

In my code, I try to get this item from the current context mapped by glass with the following line of code:

var serviceConfig = (new SitecoreContext()).GetItem<ServiceConfiguration>("{5436EEC6-1A4D-455F-8EF7-975C51FAE649}", inferType: true);

According to the documentation on inferred types, I would expect that serviceConfig would be of type JavascriptServiceConfiguration, but it is of type ServiceConfiguration. Am I missing something? I did not make some specific configuration to glass.

like image 685
Kevin Brechbühl Avatar asked Jan 06 '14 09:01

Kevin Brechbühl


1 Answers

Before types can be inferred they have to be loaded by Glass.Mapper. The more recent version of Glass loads types as and when they are requested but this won't work for inferred types. To resolve this you can force Glass to load the types when the application starts.

First find the GlassMapperScCustom class in your solution. You should then update the GlassLoaders method:

    public static IConfigurationLoader[] GlassLoaders()
    {
        var attributes = new AttributeConfigurationLoader("Your assembly name");

        return new IConfigurationLoader[] {attributes };
    }

Let me know if this doesn't fix it.

like image 108
Michael Edwards Avatar answered Oct 12 '22 22:10

Michael Edwards