Glass Mapper v4 InferType Issue with Release

I can’t understand why Glass Mapper cannot distinguish an object correctly as described here. I have the following classes

 public class BC
    {
        [SitecoreId]
        public virtual ID Id { get; set; }        
    }

public class WB : BC
    {
        [SitecoreField(FieldName = "P1")]
        public virtual Glass.Mapper.Sc.Fields.Link P1 { get; set; }
    }

 [SitecoreType(TemplateId = "{XXX}", AutoMap = true, EnforceTemplate = SitecoreEnforceTemplate.Template)]
    public class AAA : WB 
    {
        public virtual string AAAP1 { get; set; }
        public virtual DateTime AAAP2 { get; set; }
    }
 [SitecoreType(TemplateId = "{VVV}", AutoMap = true, EnforceTemplate = SitecoreEnforceTemplate.Template)]
public class BBB : WB 
    {
        public virtual string BBBp1 { get; set; }
        public virtual DateTime BBBp2 { get; set; }
    }

[SitecoreType(TemplateId = "{YYY}", AutoMap = true)]
    public class RazorRenderClass: BC
    {                      
        [SitecoreChildren(InferType = true)]
        public virtual IEnumerable<WB> Children { get; set; }

        [SitecoreChildren(InferType = true)]
        public virtual IEnumerable<AAA> AAACh { get; set; }

        [SitecoreChildren(InferType = true)]
        public virtual IEnumerable<BBB> BBBCh { get; set; }
    }

in razor mode I cannot get AAA or BBB objects if I use the Children property,

 @foreach (var child in Model.Children)
    {
        if (child is BBB)
        {
            var news = child as BBB;
            <li>
                11
            </li>
        }
        else if (child is AAA)
        {
            var evt = child as AAA;
            <li>
               222
            </li>
        }
    }

More interestingly, if I use the BBBCh or AAACh property call in cshtml, I can see (when debugging) that the Children property contains the correct elements (object), but if I try to get any element from the Children property, eg

  var detailWidget = Model.Children.FirstOrDefault();

it will be passed to the WB class. What can I do about it?

+4
source share
3 answers

, 4.0.5.54.

, , AAACh BBBch. .

[SitecoreType(TemplateId = "{YYY}", AutoMap = true)]
public class RazorRenderClass: BC
{                      
    [SitecoreChildren(InferType = true, IsLazy = false)]
    public virtual IEnumerable<WB> Children { get; set; }

    [SitecoreChildren(InferType = true, IsLazy = false)]
    public virtual IEnumerable<AAA> AAACh { get; set; }

    [SitecoreChildren(InferType = true, IsLazy = false)]
    public virtual IEnumerable<BBB> BBBCh { get; set; }
}

, , AAACh BBBch . glassmapper v3 .

[SitecoreType(TemplateId = "{YYY}", AutoMap = true)]
public class RazorRenderClass: BC
{                      
    [SitecoreChildren(InferType = true)]
    public virtual IEnumerable<WB> Children { get; set; }
}
+3

. GlassMapperScCustom GlassLoaders :

return new IConfigurationLoader[]{ new AttributeConfigurationLoader("MyAssembly")};
+2

. , , w3wp , , - .

, .

:

protected List<ORM.IBaseModule> LocalModules;
protected void Page_Load(object sender, EventArgs e)
{
        var contextItem = Sitecore.Context.Item;
        var service = new SitecoreService(Sitecore.Context.Database.Name);

        LocalModules = new List<ORM.IBaseModule>();
        foreach (Item child in contextItem.Children)
        {
            if (child.TemplateID == ORM.AAAConstraints.TemplateId)
            {
                LocalModules.Add(service.Cast<ORM.AAA>(child));

            }
            //repeat for each type of child
         }
0

Source: https://habr.com/ru/post/1607895/


All Articles