How to generate code in visual studio from uml modeling project depending on stereotype instead of element type?

StackOverflow https://stackoverflow.com/questions/22628079

Question

I have class diagram with lots of classes and interfaces.

I would like to generate code for some classes by my custom templates (about 10 templates). In order to do it now I have to specify my template for each "special" class.

But I don't want to copy-paste template bindings. Instead I want to set it on root level and set for example stereotype for my special classes so that Code Generation will apply the templates only for specific classes. How can I achieve it?

Was it helpful?

Solution

I found an answer! The only thing I need to do is to override method that checks if my template can be applied to any UML element. I added the next code to my t4 template file to check that stereotype "auxiliary" is applied to the class:

<#+
    protected override bool AppliesToElement()
    {
        bool isTemplate = this.Element.IsTemplate;
        bool isBoundElement = this.Element.TemplateBindings != null && this.Element.TemplateBindings.Any();
        return !isTemplate && !isBoundElement && GetStereotype(this.Element) == "class"
            && this.Element.AppliedStereotypes.Any(s=>s.Name == "auxiliary");
    }
#>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top