sitecoresitecore8glass-mapper

Logging/Debugging Mapping Errors in Glass Mapper SC 4.0.2.10


Does anyone know of a way to force Glass Mapper SC to throw exceptions for mapping errors? It appears to swallow them, and I'm left with null properties and no easy way to diagnose the problem. The tutorials don't really dive deep into attribute configuration, so I'm forced to do a lot of TIAS which slows down development.

I'd also settle for any method that other users have found helpful for diagnosing mapping issues.

Example Here is the template for the items I'm retrieving and attempting to map:

UnitPlantRawOrigin Template

Here is one of the items that I am returning with my query:

Item with Sample Data

Here is the model that matches the template:

[SitecoreType(AutoMap = true)]
public class UnitDetails 
{
    //[SitecoreField("ID"), SitecoreId]
    public virtual Guid ID { get; set; }

    [SitecoreField("Pre-Recycled Percentage")]
    public virtual decimal PreConsumerRecycledPercentage { get; set; }

    [SitecoreField("Post-Recycled Percentage")]
    public virtual decimal PostConsumerRecycledPercentage { get; set; }

    public virtual Plant Plant { get; set; }

    [SitecoreField("Raw Material")]
    public virtual RawMaterial RawMaterial { get; set; }

    [SitecoreField("Raw Material Origin")]
    public virtual RawMaterialOrigin RawMaterialOrigin { get; set; }
}

Even if you forget the RawMaterial and RawMaterialOrigin properties for the moment (those don't map either), the decimal properties do not map. Also, the ID property will always be null unless I name it exactly (ID). I thought the [SitecoreField("ID"), SitecoreId] decorator was supposed to provide the hint to Glass. Here is an example of the mapped data. No exception is thrown:

Debug Screenshot - Unmapped Properties


Solution

  • I understand this is old thread and might have resolved already, but as I managed to resolve this one more time (forgot to update last time :D) thought of recording this time.

    I was doing upgrade to v5 of glass mapper. I followed attribute based configuration which is default. It is documented here, but on top of that I add

    1) Templates on classes

    [SitecoreType(AutoMap = true, TemplateId = "<Branch Id>"]
    

    2) Id field should be declared as following in your code.

    [SitecoreId]
    public virtual Guid Id { get; set; }
    

    3) Sitecore service changes as mentioned in the article using Sitecore Service (MVC / WebForm), passed lazy load as false and infer type as true in all places. This was really important step.

    I hope this will help me next time I visit this issue. :D