Considerations when managing source control via Visual Studio Team Foundation Server and using Text Template Transformation Toolkit to generate .NET4 Entity Framework objects

T4 background

Leveraging the power of Text Template Transformation Toolkit (T4 template) to generate code (or any other type of text) is a great time saver and it is particularly useful when working with Entity Framework (EF) since it allows to easily (almost in a batch like fashion) do things like creating Plain Old CLR Object (POCO), ADO.NET objects and as mentioned in a previous blog, compiled views (a simple search on “Entity Framework T4 templates” will result in lots of these samples). T4 templates are not particular to EF, since they can be leverage to generate any other type of code (or text) - they help in the creation of anything where lots of text is needed (code) based on a pre-defined set of rules and some given parameters/properties. The following, describes some problem scenarios which maybe experienced when using T4 templates to generate object layer code when working with large models and managing source control via Visual Studio Team Foundation Server.

The scenario

Extracting from a customer engagement, take the creation of POCO objects via a T4 template. As it will be expected, it generates the appropriate .CS files (in the case of C#) for every entity in your model which in many of our customer’s scenarios will translate into a large amount of files. Now, if Visual Studio Team Foundation (VSTFS) is been leveraged as the source control, then the tendency will be to simply check in all of these newly generated objects, which in itself is a fairly harmless situation. The problem will arise if any of the following actions is done while working with large amount of entities in your model, these actions will cause huge amounts of updates to the generated code files, this can cause a large amount of Check-ins in the source control which, in turn, will negatively slow down the whole VSTFS server (others working on the system will also experience the effect).

· Any entity changes/updates - this simple act can trigger the T4 template to re-generate all the code files, even if the changes were limited to some files/entities, if you have 500 entities that will translate in 500 files been checked-in

· T4 template code itself is changed - all the generated objects will also require re-generation.

·        T4 template is deleted - this will trigger more actions on the VSTFS server as well.

Hence, a simple edit action can significantly create long periods of synchronization time across all VSTFS users. This is not an independent issue with T4 POCO template or the Entity Framework or large models, but the consequence of having the combination of the three easily generating many files needing to be source controlled.

Workarounds

· Split your EF model - this will minimized the total amount of file been re-generated

· Lock down the modification of the model - this way the long check-in times can be scheduled

· Leverage the Team Foundation Server Power Tools – Since not all the re-generated files are necessarily changed files (an entity update maybe limited to some entities but the re-generation will affect all files), the ability to undo unchanged files will be very useful, the tfpt.exe command line tool will allow doing exactly this when using the “uu” flag, as follows:

· Create a separate assembly for the model and have a separate project for the T4 template which will reference the separate assemblies, this way, changes will not propagate until manually triggered. This is similar to the point above, since it will allow scheduling of check-in times.

· Modify the T4 template to generate fewer files, less files to re-generate translates to less check-ins

· Selecting all of the files that will be re-generated and check them all out (right click and choose ‘Check Out for Edit…’) , do the required edits, let them re-generate and then scheduled all your check-ins

Conclusion

The power of T4 templates is great and should be leverage where it makes sense but if a large amount of code is been generated then the development environment should be taken into consideration, more so when a source control system is used. In general terms, the solution is to minimize the amount of changes, the amount of generated files or find ways to perform large updates in a scheduled manner.

Created by: Jaimeab

Reviewed by: Suren Machiraju