2

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

4
  • This looks like it is an issue with the T4 templates, which to be fair are pretty old and from before this multi-project feature was introduced so they may not have been updated. If your models are in the same project then there is no issue with the namespaces (which is probably how it was intended to be used)
    – jammykam
    Jul 31, 2015 at 16:18
  • Thx for the comment. Yes models in the same project work just fine. But we wanted to keep the base templates separate and plan on sharing them across different websites.
    – Rohan
    Aug 2, 2015 at 16:59
  • That's understandable and makes perfect sense for separation of projects. Thanks for sharing your solution.
    – jammykam
    Aug 2, 2015 at 21:17
  • 1
    Has anyone been able to come up with a better solution than the hardcoded approach below?
    – Jeroen
    Sep 19, 2017 at 1:38

2 Answers 2

3

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("_", "");
}
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.