0

我们正在开发一个多站点站点核心解决方案,其中每个站点都可以拥有自己的新闻,并且能够显示来自其他站点的组合新闻。

问题:每个站点都有其独特的新闻需求,其中 90% 的模板字段匹配,但其余 10% 不同。

例如,站点 A 具有带有作者下拉列表的新闻模板,其中作者列表是在配置节点上创作的。Site-B 有新闻模板,其中作者是免费文本字段。

因此,当 Glass Mapper 自动尝试映射作者字段时,自由文本字段会失败。

解决方案:这可以通过在所有站点上创建一个作者作为下拉列表来解决,但产品所有者不希望这样做。

另一种解决方案是手动映射来自两个来源的新闻字段或使用 AUTOMAP 等。

期望的解决方案:Glassmapper 自动解析并即时填充作者文本字段或下拉字段。

以上可以吗?

谢谢你。

4

3 回答 3

1

您可以按如下方式使用 Infer 类型:

public interface IBaseNews
{
    string Author {get; set;}
    //List all other shared fields below
}

[SitecoreType(TemplateId="....", AutoMap = true)]
public class NewsSiteA : IBaseNews
{
    [SitecoreField]
    public string Author {get; set;}

    //List all fields which are unique for SiteA

}

[SitecoreType(TemplateId="....", AutoMap = true)]
public class NewsSiteB : IBaseNews
{
    [SitecoreField]
    public string Author {get; set;}

    //List all fields which are unique for SiteB

}

现在,您的代码应该是:

IBaseNews newsClass = NewsItem.GlassCast<IBaseNews>(true,true);
//You can use Author property now
于 2014-12-21T15:21:53.777 回答
1

首先,出于包括委托功能在内的许多其他原因,我建议您更新到最新版本的 Glass。

从评论中的推断类型示例 - 您不应该使用 GlassCast,使用来自 sitecore 服务/上下文的 CreateType(Item item)。如果您采用带有 Delegate 的版本,现在在 sitecore 服务上有一个正式的 Cast(Item item)。

此外,那里使用的示例 a 也无法解决类型的差异。代表将使这非常容易。请记住,委托没有延迟加载,在这种情况下这无关紧要。

public interface INews
{
    // All my other fields
    string Author { get; set; }
}

流畅的配置类似于(在 GlassScCustom 中完成)

SitecoreType<INews> = new SitecoreType<INews>();
sitecoreType.Delegate(y => y.Author).GetValue(GetAuthor);
fluentConfig.Add(sitecoreType);

private string GetAuthor(SitecoreDataMappingContext arg)
{
    Item item = arg.Item;
    if(item.TemplateID == <templateid>)
    {
        // return the value from the drop link
    }

    return item["Authors"];
}
于 2014-12-28T09:14:08.873 回答
1

我会通过“流利的配置”来解决这个问题,http ://glass.lu/Mapper/Sc/Tutorials/Tutorial8.aspx 。结合最近添加到 Glass Mapper 中的新委托功能。委托功能最初是在此处介绍和描述的:http: //cardinalcore.co.uk/2014/07/02/controlling-glass-fields-from-your-own-code/

代表功能的 Nuget 包:https ://www.nuget.org/packages/Cardinal.Glass.Extensions.Mapping/

于 2014-12-21T13:25:50.590 回答