1. Visual Studio 2010Effective Code Generation Techniques Doug Holland doug.holland@microsoft.com blogs.msdn.com/dohollan
2. Doug Holland Sr. Architect Evangelist, Microsoft Corporation MSc Software Engineering, Oxford University @dougholland http://blogs.msdn.com/dohollan
3.
4. Agenda StringBuilder System.CodeDom Code Snippets XSLT Visual Studio 2010 Project & Item Templates Text Template Transformation Toolkit (T4) Entity Framework 4.0 & T4 Templates UML & T4 Templates SQL Server 2008 R2 Management Objects (SMO) Unit Testing
5. StringBuilder Advantages Almost all .net developers should feel comfortable using the StringBuilder class so it requires no additional languages / tools to learn. Even with the disadvantages, the StringBuilder can be an excellent choice if you need to generate code quickly. Especially if you don’t need to maintain the “template” long term. Disadvantages Unable to truly see the “template” within the control logic. Requires a recompile if the “template” should be changed.
6. System.CodeDom Advantages Allows a single “template” to generate code within several target languages, such as VB and C#. Additional language providers can be written to support other languages. Proven technology, used by Microsoft for code generation in all versions of Visual Studio since .net was released. Disadvantages Unable to truly see the “template” within the control logic. Requires a recompile if the “template” should be changed. Very few developers have experience using the CodeDom even though it has been with us since the .net framework 1.0.
7. Code Snippets Advantages Simple Xml based template with numerous samples provided within Visual Studio. Several open source code snippet libraries available. Disadvantages Limited snippet functions and at the present time developers are unable to author additional snippet functions! Tell the Visual Studio team you want to be able to author snippet functions, tell your Congressman too if you think it would help!
8. XSLT Advantages Xml based template. Simple code generation technique for those developers comfortable with XSLT transformations. Disadvantages While Xml is a relatively simple technology, XSLT has been known to bring grown men to tears! XSLT can be difficult to debug.
9. Visual Studio Project / Item Templates Advantages Target language used to write the templates. (e.g. C# or VB). Simple Xml *.vstemplate file tells Visual Studio what to do with the template. Templates are stored within .zip file which is a standard file format we’re all familiar with. Disadvantages Templates are stored within .zip file which can complicate editing templates.
10. T4 Templates Advantages Ability to embed C# control logic within the template. Syntax that will be somewhat familiar to classic ASP developers.. Preferred code generation technique for Entity Framework 4.0 and the Visual Studio 2010 modeling tools., which means lots of samples on MSDN, etc. Templates can be precompiled. Disadvantages While third parties have written language support for Visual Studio no built in support for editing T4 templates exists in Visual Studio today. Tell the Visual Studio product team and your congressman about this too! Relatively new technology, therefore not many developers have experience with T4 templates.