Reputation: 51
I am trying to set multi-project properties exactly like how it says in this link/article with base template reference to another TDS project. http://hedgehogdevelopment.github.io/tds/chapter4.html#multi-project-properties
Similar to the above link I have TDS Project TDS A that generates code in project X and TDS Project B (Base templates) that generates code in project Y. Project X references Y and TDS Project A references Project B in the Multi-project properties setting.
Sounds like I am doing what the article says. But the code generated from the TDS project A is not able to generate references to code generated by TDS Project B. To give an example of whats happening: So the right behavior is that class generated in Project X say Class D should inherit from base class say Class Base from Project Y, instead it creates its own version of fully qualified namespace for Class Base that does not exists. It uses its own assembly namespace ProjectX.tree.structure.BaseClass, when it should be ProjectY.tree.structure.BaseClass.
Has anyone got this to work. Am I missing something ?
I have got it to work by tweaking the T4 templates, but that's not the best solution
Thanks
Upvotes: 2
Views: 657
Reputation: 81
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!
Upvotes: 0
Reputation: 51
Someone on the team helped me with this. This is not the best solution but should work if you have a setup as defined above. Trick is to modify the below method in the Helpers.tt template. Add the line marked with the comment. Should be able to extend this little further to not hardcode the base project namespace. Will post if I get around to figuring that.
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("_", "");
}
Upvotes: 3