Sitecore TDS 多项目属性基本模板参考对我不起作用

Sitecore TDS Multi-Project properties base template reference not working for me

我正在尝试设置多项目属性,就像它在 link/article 中所说的那样,并使用对另一个 TDS 项目的基本模板引用。 http://hedgehogdevelopment.github.io/tds/chapter4.html#multi-project-properties

与上述类似 link 我有 TDS 项目 TDS A 在项目 X 中生成代码,TDS 项目 B(基本模板)在项目 Y 中生成代码。项目 X 引用 Y 和 TDS 项目 A 引用多项目属性设置中的项目 B。

听起来我正在按照文章所说的去做。但是从 TDS 项目 A 生成的代码无法生成对 TDS 项目 B 生成的代码的引用。举个例子说明发生了什么: 所以正确的行为是 class 在项目 X 中生成说 Class D 应该从项目 Y 的基础 class 说 Class 基础继承,而不是它为不存在的 Class 基础创建自己的完全限定名称空间版本。它使用自己的程序集命名空间 ProjectX.tree.structure.BaseClass,而它应该是 ProjectY.tree.structure.BaseClass。

有没有人让这个工作。我错过了什么吗?

我通过调整 T4 模板让它工作,但这不是最好的解决方案

谢谢

团队中有人帮助我解决了这个问题。这不是最好的解决方案,但如果您有上面定义的设置,应该可以使用。技巧是修改 Helpers.tt 模板中的以下方法。添加标有注释的行。应该能够进一步扩展这一点,而不是对基础项目命名空间进行硬编码。 post 如果我有时间弄明白的话。

public static string GetNamespace(string defaultNamespace, SitecoreItem item, bool includeGlobal = false)
{
    List<string> namespaceSegments = new List<string>();
    // add the following line
    namespaceSegments.Add(!item.ReferencedItem ? defaultNamespace : "[BaseProjectNameSpace]");
    namespaceSegments.Add(item.Namespace);
    string @namespace = AsNamespace(namespaceSegments); // use an extension method in the supporting assembly

    return (includeGlobal ? string.Concat("global::", @namespace) : @namespace).Replace(".sitecore.templates", "").Replace("_", "");
}

这个 post 已经很老了,但是我相信我已经找到了解决方案。我开始挖掘 GlassV5Item.tt。当模板生成继承字符串时,它调用了一个方法GetObjectInheritanceDefinition(DefaultNamespace, template, true, (string s) => AsInterfaceName(s))。该方法如下所示:

<#+
/// <summary>
/// Gets the inheritance string for the generated template
/// </summary>
/// <param name="defaultNamespace">The default namespace.</param>
/// <param name="template">The template to get the bases for.</param>
/// <param name="nameFunc">The function to run the base templates names through.</param>
/// <returns></returns>
public static string GetObjectInheritanceDefinition(string defaultNamespace, SitecoreTemplate item, bool includeLeadingComma, Func<string, string> nameFunc)
{
    if (item.BaseTemplates.Count > 0)
    {
        return string.Concat(includeLeadingComma ? ", " : "",
                                item.BaseTemplates
                                .Select(bt => GetFullyQualifiedName(defaultNamespace, bt, nameFunc)) // select the name of the template with an 'I' prefix
                                .Aggregate( (total,next) => total + ", " + next) // basically a string.join(string[], '')
                            );
    }
    return "";
}

代码中有问题的部分是 .Select( bt => GetFullyQualifiedName(defaultNamespace, bt, nameFunc))

模板不尝试解析基本模板的命名空间。我通过更改 select 解决了这个问题,如下所示: .Select(bt => GetFullyQualifiedName(bt.TargetProjectName, bt, nameFunc))

我不确定 TargetProjectName 是否是最好的 属性 使用,但由于我们的项目是基于 Helix 的,因此 TargetProjectName 名称和该项目的名称空间匹配。

这里最大的关键是继承的命名空间是从项目派生的,而不是从硬编码的参数派生的,正如您所说的那样是一个问题。

希望对您有所帮助!