T4 custom code blocks - t4

With T4 can you have custom code blocks, that should not be overwritten at generation?
Like PreserveExistingFile but for a block of code in the generated file.
I can’t use partial class in my scenario

There's no way to do this. You should either include unmodifiable code blocks in the template itself, or read them from another file.

Related

How to define data model for include template in freemarker

I am trying to process the freemarker template through template.process(,) and loading the template from database(mongo) through the TemplateLoader interface.Here I want to define the different data-model for the included template. Mainly I want to define template.process for an individual included template.
Also, is there any way through which we can execute the business logic while processing the template.
Will it be a good idea to process the template while implementing the templateloader interface?
If by including a template you mean using <#include ...>, that's basically for composing a single template from reusable fragments, so it can't have a different data-model than the including template. But you could use your own custom directive implementation (see TemplateDirectiveModel) to do a whole new template processing inside another, just pass Environment.getOut() to Template.process so that it writes to the output of the parent template processing.

Change modified classes generated by T4

I used T4 to generate some entity classes , but I forgot to make them Serializable. So is there any solution to use something like T4 to add a Serializable attribute to all my classes ?
If you've already modified your generated classes, I think you might find it easier to do with Visual Studio's global replace with a fancy regex to find the classes you need to change. (If that's not possible, it's not hard to write a quick console application to process the files).
Using T4 you can control which files are overwritten, for instance using the Output.PreserveExistingFile which comes with T4 Toolbox.
<#
var t = new SampleTemplate();
t.Output.File = "Sample.cs";
t.Output.PreserveExistingFile = true;
t.Render();
#>
And then you can delete the specific files you want recreated. But however you determine which files to overwrite, any changes to those files that you've made since last regenerating will be lost. One recommendation is to build your templates as partial classes so that you can put all manual modifications in a separate file (but that doesn't really help you if you've already modified your generated classes).
Are those generated classes partial classes? If so, use another T4 template in order to generate a partial class definition decorated with the Serializable Attribute.
Otherwise you could use the Visual Studio CodeModel in order to identify all classes that need this implementation inside another T4 template and then let this T4 template add the code fragments necessary.
If you are using tangible's T4 Editor, it comes with a free Template Gallery and as far as I know there is a template called "Add NotifyPropertyChanged" which does pretty much what you are looking for: discovering code classes inside a Solution and making them implement a given interface. You might adapt that one easily and get your desired functionality.
Hope that helps.

How do I inlude a jsp file within another file?

I know how to import classes and libraries, if I have header.jsp and I want to include it within my index.jsp... I've been looking into the #page annotation but am having trouble finding a list of parameters.
Is this the wrong approach altogether? If I'm going to be setting up header and footer files should I use a completely different method?
You can include a jsp page in another either statically or dynamically. Each has its own syntax and usage recommendation. In your case, I believe static include is what is needed.

Visual Studio code generation - how to deal with developers editing class files

So thanks to the Visualization and Modeling Feature Pack , I can build a uml model diagram and generate a bunch of classes.
But what now? Presumably, my developers will add code to those classes. Useful code, valuable code, and as the templates themselves indicate:
// Changes to this file will be lost if the code is regenerated.
So what is the best solution here? Can I make the modeling project reflect changes to the actual classes? Should I generate partial classes? Modify the default templates to read class files and not auto-generate anything that has been modified? Should I tell developers not to edit model files under pain of....well, pain?
Thanks for the tips.
As far as I know, this is really the key reason for partial classes in the first place. The custom code goes in one file, the auto-generated in another.
You could also create classes derived from the generated ones, and put any changes in there. I also agree with above poster that partial classes could be the way to go.
Although the tools generate basic skeleton classes out of the box, that's really just a starting point. You can easily adapt the generator templates to create your own stuff. Different people want to generate different code from the classes - some even generate XML or SQL. And yep, in C#, partial classes are good to generate, so's to keep the hand-written code separate from the generated bits.
It's good to put lots of extension points in the generated code, where you fill in the details by hand code.
Another neat idea is "double derived": from each UML class, generate a base class and a derived class. The derived one has only constructors. The base class has any methods you generate. So your hand code can easily override generated methods where you need that.
There are several options in the tool and recommending what is best is hard without knowing your scenario. Partial classes are great for some, but not all applications. If you want your UML class to generate a partial class, you can set it's C# stereotype's property to "Partial" and it will do so, and custom code can then be added in a partial class that won't be overwritten. If you want to prevent code from being overwritten, you can do this by setting the overwrite property to False on the template binding that corresponds to the package you are working on. This lets you set your extension code to be in a package that is not overwritten, while your model mastered code is overwritten with the latest model changes. Finally, if you want your code to be the master for your model so it always reflects the latest code, then you can reverse engineer your code by using the architecture explorer to select your classes and then dragging them in to a UML diagram. So for a given gesture, either the model is the master or the code is the master. In this version, we did not implement automated merge capabilities between the two.

How to read a DBML file and map it into an object model in .NET?

I'm planning to write a code generator to generate UI (forms, grids, etc.). Since I'm using LINQ I'm planning to read a DBML file for metadata extraction purposes. Is there some API to read the DBML object model (database, tables, columns, and associations)?
I've opened SQLMetal.exe with Red Gate Software's .NET Reflector. It contains a namespace called LinqToSqlShared.DbmlObjectModel and it seems it contains everything I need to read a DBML object model. The bad news is: that namespace is not exposed for external consumption and all classes are declared as "Friend" (or sealed).
Once again, does something exist, like the LinqToSqlShared.DbmlObjectModel namespace, to read DBML to an object model in .NET.? Or do I have to write it my self?
Have a look at how the model is read in L2ST4 project. You could reuse the T4 file.
Take a look at a tool like Reegenerator. We're considering this as a replacement for the L2S code generator, because we've got some specific code generation requirements.

Resources