Question about Code Generation Extensions

Topics: Service Factory Modeling Edition Forum
Nov 16, 2007 at 4:55 AM
Edited Nov 16, 2007 at 4:55 AM
I was reading the help about the Code Generation Extensions and it mentions that an artifact link could have more than one T4 Template associated with it. So I figured I would add a new T4 Template to the ServiceLink to generate a second file from the artifact. I recompiled everything, launched the factory and kicked off the code generation, but I did not get the second file. So I launched the factory in the debugger and walked through the code generation source and noticed that the TextTemplateCodeGenerationStrategy only generates the first template associated with an artifact, even though they are both for the same language associated with the project.

So what is the best way get the code generation extensions to generate multiple files from a model element? And what if I want to have files generated in multiple projects. Do I need to create another artifact link for the model element for each file I want to generate?

Looking for some guidance.
Nov 16, 2007 at 3:09 PM
Generally there is a 1:M mapping between model elements/extenders and artifact links and a 1:1 mapping between artifact links and text templates. If the document says otherwise then it's erronious. The TextTemplateCodeGenerationStrategy implementation is only expecting one template (although obviously you're free to modify this).

If you want to add a second template an approach more in line with our architecture would be to add another artifact link to the Service and generate a second file from that.

Does this answer your question?

Ade
Nov 16, 2007 at 9:07 PM
Edited Nov 16, 2007 at 9:08 PM
Thanks I'd rather keep in line with the existing architecture. What wasn't apparent was the fact that the object extenders actually use reflection to find all of the properties of that derive from artifact link in order to find all of the templates to use in generating code.

It looks as if the default project role mapping that is associated with the model element in the DSL is the default that is setup for where the template output is saved. I am assuming that I should be able to change that to a different project role mapping if I want the template output to be saved elsewhere.

Is there a better way to redirect the template output to a different project?

Jim
Nov 18, 2007 at 5:02 PM
I don't think so. The primary mechanism for directing output of code generation is model element roles. The factory author sets these up and the factory user sets the role-project/folder mapping using the Project Mapping Table (PMT).

I think this meets your requirements or are you trying to generate two different artifacts in different projects from the same model element?

Ade
Nov 19, 2007 at 3:54 PM


AdeMiller wrote:
I don't think so. The primary mechanism for directing output of code generation is model element roles. The factory author sets these up and the factory user sets the role-project/folder mapping using the Project Mapping Table (PMT).

I think this meets your requirements or are you trying to generate two different artifacts in different projects from the same model element?

Ade


Ade,

I am actually trying to generate two different artifacts in different projects from the same model element. Is there any way to do this without having to circumvent the existing architecture?

Thanks

Jim
Developer
Nov 19, 2007 at 10:09 PM
Jim,

As Ade said, using the 1:M relationship between model elements and artifact links you should be able to generate M objects.
The problem here is that each model element only accepts one role, therefore on target project.

However, there is an example built in that actually generate two links into two different projects. That is the case of the "XsdMessageContract".
In this case, the XsdMessage model element is marked with the MessageContractRole and the Element property of this model element is marked with DataContractRole.
Then if you look at the "WCFXsdMessageContract" class in the WCF extender library, you will notice that it has two artifact links, one for the XsdMessage and the other for the Element property. Then you will have two different links with each template and they will generate code to different projects.

Again, consider this as an "edge case" since is not the standard approach but you still have an option for this scenario that is actually implemented in the factory.

Thanks,
CF