2

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

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

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

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

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

谢谢

4

2 回答 2

3

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

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("_", "");
}
于 2015-07-31T16:48:28.027 回答
0

This post is pretty old, however I believe that I have found a solution. I started digging through the GlassV5Item.tt. When the template generates the inheritance string, it calls a method GetObjectInheritanceDefinition(DefaultNamespace, template, true, (string s) => AsInterfaceName(s)). That method looks like this:

<#+
/// <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 "";
}

The offending section of code is the .Select( bt => GetFullyQualifiedName(defaultNamespace, bt, nameFunc)).

The template makes no attempt to resolve the namespace of the base template. I resolved this by changing the select, as follows: .Select(bt => GetFullyQualifiedName(bt.TargetProjectName, bt, nameFunc))

I'm not sure if TargetProjectName is the best property to use, but since our project is Helix based, the TargetProjectName name and the namespace of that project match.

The biggest key here, is that the inherited namespace is being derived from the item, not from a parameter that's hard-coded, as you called out as an issue.

I hope this helps!

于 2020-03-30T22:04:18.967 回答