Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top