Glass Mapper v4 InferType Cast问题

时间:2015-09-17 19:28:57

标签: c# sitecore glass-mapper sitecore-mvc

我无法理解为什么Glass Mapper无法以正确的方式投射对象,如here所述 我有下一堂课

 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; }
    }

在razor视图中,如果我使用Children属性

,我无法获得AAA或BBB对象
 @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>
        }
    }

更有趣的是,如果我在cshtml上使用对BBBCh或AAACh属性的调用,我可以看到(在调试时)Children属性包含正确的项目(对象)但是如果我尝试从Children属性中获取任何项目

  var detailWidget = Model.Children.FirstOrDefault();

它会转向WB类。 我能用它做什么?

3 个答案:

答案 0 :(得分:4)

我遇到完全相同的问题,版本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; }
}

答案 1 :(得分:3)

我也遇到过这个问题。将它添加到GlassMapperScCustom的GlassLoaders方法中为我修复了它:

from multiprocessing import Pool

def updateDB(ip):
     code goes here...

def my_view(request):
    pool = Pool(processes=4)              # process per core
    pool.map(updateDB, ip)
    return HttpResponse("SUCCESS")

答案 2 :(得分:0)

添加配置加载器只能间歇性地为我修复它。基本上,任何时候w3wp都会循环,有些类型是正确的,有些类型是Castle Proxies。

我认为这是最新版本中的一个错误。

我的解决方法是使用代码重新绑定它:

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
         }