Odessa .NET User Group - 10.11.2011 - Applied Code Generation


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Odessa .NET User Group - 10.11.2011 - Applied Code Generation

  1. 1. APPLIED CODE GENERATION IN .NET Dmytro Mindra Lohika, Odessa @dmytromindra
  2. 2. @dmytromindra
  3. 3. The question of Origin ?• We are engineers !• Engineers’ time is valuable and expensive;• Engineers don’t like repetitive tasks; @dmytromindra
  4. 4. Task classification• Brains• Experience• Procedures @dmytromindra
  5. 5. Houston, we’ve got a problem !Our business domain contains 500+ entities. Each entity needs CRUD. @dmytromindra
  6. 6. Houston, we’ve got a problem ! And you have lots of time. The whole 4 months.For your big team of 4 developers that should not be a problem ! @dmytromindra
  7. 7. Houston, we’ve got a problem !Oh, and we’re not sure which parts will stay generic and which won’t @dmytromindra
  8. 8. Houston, we’ve got a problem !Be agile, don’t wait for the analysts to finish theirjob, because we don’t have that kind of time.Just change your code when the Requirements arefinished. @dmytromindra
  9. 9. Houston, we’ve got a problem !For each domain entity we should have• Stored Procedures• Repository• Service• Controller• Views• etcThat’s about 1000 lines of code per entity. @dmytromindra
  10. 10. Houston, we’ve got a problem !But we have 500+ entities500*1000 = 500000 lines of code.Who wants to type this in ? @dmytromindra
  11. 11. Houston, we’ve got a problem !• And when the changes arrive someone should make corrections to 500*1000 lines of code. @dmytromindra
  12. 12. What are the solutions ?• Hand-code everything (and every change) – Hire a lot of monkeys coders• Design everything generically, correctly the first time – Mission impossible• Use only code generation – And spend your life changing generator to match all possible requirements• Use combination of tools including code generation. – This seems reasonable @dmytromindra
  13. 13. Boilerplate• Interestingly, the term (boilerplate) arose from the newspaper business. Columns and other pieces that were syndicated were sent out to subscribing newspapers in the form of a mat (i.e. a matrix). Once received, boiling lead was poured into this mat to create the plate used to print the piece, hence the name boilerplate. As the article printed on a boilerplate could not be altered, the term came to be used by attorneys to refer to the portions of a contract which did not change through repeated uses in different applications, and finally to language in general which did not change in any document that was used repeatedly for different occasions. @dmytromindra
  14. 14. @dmytromindra
  15. 15. CODE GENERATION is code that writes code @dmytromindra
  16. 16. Some common terms• Code generation• Automatic programming• Generative programming @dmytromindra
  17. 17. IDE Generated Code Example @dmytromindra
  18. 18. Tools evolution @dmytromindra
  19. 19. Code Smithhttp://www.codesmithtools.com/ Demo @dmytromindra
  20. 20. GeneratorInput Code Output Generator @dmytromindra
  21. 21. Why CG?• Productivity• Quality• Consistency• Abstraction @dmytromindra
  22. 22. @dmytromindra
  23. 23. PROS• Code uniformity• Easy to debug• Easy to introduce huge changes• No more boilerplate code @dmytromindra
  24. 24. CONS• Someone should maintain the generator• Generator has learning curve• Generator usually has limitations @dmytromindra
  25. 25. Applications• Database Access• Generate Test Data• Generate Integration Tests• User Interface• Documentation• Unit tests• File Formats• Web services• Business logic• DLL Wrappers• Firewall configuration• Website configuration• Looking for localizable strings @dmytromindra
  26. 26. Code Generation is Fun ?• CG makes boring job for you• Writing generator is fun• Once written generator works for you• You can experiment with different implementations by modifying generator and regeneration the code.• While writing the generator you get deeper into the technology instead of doing dull job. @dmytromindra
  27. 27. Scaffolding and prototyping• Code generation can help you to build fast prototypes. @dmytromindra
  28. 28. MVC Scaffolding Demo @dmytromindra
  29. 29. Rules1. Give the proper respect to hand-coding2. Handwrite the code first3. Control the source code4. Make a considered decision about the implementation language5. Integrate the generator into the development process @dmytromindra
  30. 30. Rules6. Include warnings7. Make it friendly8. Include documentation9. Keep in mind that generation is a cultural issue10. Maintain the generator @dmytromindra
  31. 31. A note about DSLA domain-specific language (DSL) is aprogramming language or specificationlanguage dedicated to a particular problemdomain @dmytromindra
  32. 32. LOGO@dmytromindra
  33. 33. Model Driven Architecture @dmytromindra
  34. 34. @dmytromindra
  35. 35. Model Driven Architecture• Code generation has a dramatic impact on development time and engineering productivity.• The application is amenable to change on a large scale.• The business rules are abstracted into files that are free of language or framework details that would hinder portability.• The code for the application is of consistently high quality across the code base. @dmytromindra
  36. 36. Benefits of MDA• Quality• Consistency• A single point of knowledge• More design time• Design decisions that stand out @dmytromindra
  37. 37. Software Factories @dmytromindra
  38. 38. Industrializing Software Development @dmytromindra
  39. 39. Software Factory @dmytromindra
  40. 40. Consider this:• Code generation has a large initial schedule overhead;• Code generators are ideal for well-known large- scale problems;• Code generation is powerful when used appropriately;• Always keep in mind code generation limitations! @dmytromindra
  41. 41. Code Generation Skills• Text Templates• Regular Expressions• XML Parsing• Reflection @dmytromindra
  42. 42. Show methe tools @dmytromindra
  43. 43. Available tools• StringBuilder• CodeSnippets• XSLT• Reflection Emit• EnvDTE• CodeDom• T4• … (Python, Ruby, @dmytromindra
  44. 44. 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. @dmytromindra
  45. 45. 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! @dmytromindra
  46. 46. 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. @dmytromindra
  47. 47. Reflection EmitIt is rocket science mixed with the black arts.• Full representation of physical structure• Allows building modules and assemblies at runtime – Transient code only used at runtime – Persistent code for reuse• Create classes, types and emit IL• Used by .NET compilers to build .NET apps• Can be as fast as normal code @dmytromindra
  48. 48. EnvDTE• EnvDTE is an assembly-wrapped COM library containing the objects and members for Visual Studio core automation.• Using EnvDTE you can develop your own add- ins for Visual Studio that can generate code. @dmytromindra
  49. 49. 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. @dmytromindra
  50. 50. TextTemplateTransformationToolkit @dmytromindra
  51. 51. How T4 Works ? @dmytromindra
  52. 52. T4 Tools• Tangible T4 Editor for Visual Studio 2010 RTM adds IntelliSense and Syntax Coloring to T4 Text Templates.• T4 Toolbox @dmytromindra
  53. 53. T4 Pros and Cons• 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. – Relatively new technology, therefore not many developers have experience with T4 templates. @dmytromindra
  54. 54. ReSharper 6.0 Code GenerationCreate from UsageGenerate Type MembersGenerate Type ConstructorGenerate PropertiesGenerate Delegating MembersGenerate Formatting MembersGenerate Equality MembersImplement/Override Methods @dmytromindra
  55. 55. Some advices @dmytromindra
  56. 56. Shall I Generate Interfaces? @dmytromindra
  57. 57. Shall I Generate Interfaces?No ! @dmytromindra
  58. 58. Shall I Unit Test generated code ?• You shouldn’t unit test generated code. You may unit test generator instead. @dmytromindra
  59. 59. Real WorldCase Studies @dmytromindra
  60. 60. Case 1: Client – Server Communication Communic Existing Generated ation Generated API Server Proxy channel Client Proxy @dmytromindra
  61. 61. Legacy API Adapter@dmytromindra Existing Case 2: API Wrapper Application
  62. 62. Case 3: Protocol Parser @dmytromindra
  63. 63. Case 4: Model Driven DAL Hand written services Generated RepositoriesXML Generated EntitiesModel Partially generated DB @dmytromindra
  64. 64. It’s an Engineering, Baby !Conclusion: • Not a silver bullet but another good tool; • Makes life easier when applied properly; • Writing generator is much more fun than writing boilerplate code; • Generated code is easier to debug; • You can make huge changes in regenerated code easily; @dmytromindra
  65. 65. Do try this !• Code generation can make you flexible and responsive. Do try this at home. (Or at work) @dmytromindra
  66. 66. @dmytromindra