network - 15.10.2011 - applied code generation in .net
DESCRIPTION
TRANSCRIPT
![Page 1: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/1.jpg)
APPLIED CODE GENERATION IN .NET
Dmytro Mindra Lohika, Odessa
![Page 2: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/2.jpg)
![Page 3: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/3.jpg)
What’s the matter ?
• We are engineers ! • Engineers’ time is valuable and expensive; • Engineers don’t like repetitive tasks;
![Page 4: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/4.jpg)
Task classification
• Brains • Experience • Procedures
![Page 5: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/5.jpg)
Houston, we’ve got a problem !
Our business domain contains 500+ entities.
Each entity needs CRUD.
![Page 6: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/6.jpg)
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 !
![Page 7: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/7.jpg)
Houston, we’ve got a problem !
Oh, and we’re not sure which parts will stay generic and which won’t
![Page 8: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/8.jpg)
Houston, we’ve got a problem !
Be agile, don’t wait for the analysts to finish their job, because we don’t have that kind of time.
Just change your code when the Requirements are
finished.
![Page 9: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/9.jpg)
Houston, we’ve got a problem !
For each domain entity we should have • Stored Procedures • Repository • Service • Controller • Views • etc That’s about 1000 lines of code per entity.
![Page 10: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/10.jpg)
Houston, we’ve got a problem !
But we have 500+ entities 500*1000 = 500000 lines of code. Who wants to type this in ?
![Page 11: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/11.jpg)
Houston, we’ve got a problem !
• And when the changes arrive someone should make corrections to 500*1000 lines of code.
![Page 12: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/12.jpg)
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
![Page 13: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/13.jpg)
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.
![Page 14: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/14.jpg)
![Page 15: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/15.jpg)
CODE GENERATION is code that writes code
![Page 16: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/16.jpg)
Some common terms
• Code generation • Automatic programming • Generative programming
![Page 17: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/17.jpg)
IDE Generated Code Example
![Page 18: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/18.jpg)
Tools evolution
![Page 20: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/20.jpg)
Generator
Code Generator
Input Output
![Page 21: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/21.jpg)
Why CG? • Productivity • Quality • Consistency • Abstraction
![Page 22: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/22.jpg)
![Page 23: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/23.jpg)
PROS
• Code uniformity • Easy to debug • Easy to introduce huge changes • No more boilerplate code
![Page 24: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/24.jpg)
CONS
• Someone should maintain the generator • Generator has learning curve • Generator usually has limitations
![Page 25: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/25.jpg)
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
![Page 26: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/26.jpg)
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.
![Page 27: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/27.jpg)
Scaffolding and prototyping
• Code generation can help you to build fast prototypes.
![Page 28: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/28.jpg)
MVC Scaffolding Demo
![Page 29: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/29.jpg)
Rules
1. Give the proper respect to hand-coding 2. Handwrite the code first 3. Control the source code 4. Make a considered decision about the
implementation language 5. Integrate the generator into the development
process
![Page 30: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/30.jpg)
Rules
6. Include warnings 7. Make it friendly 8. Include documentation 9. Keep in mind that generation is a cultural
issue 10. Maintain the generator
![Page 31: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/31.jpg)
A note about DSL
A domain-specific language (DSL) is a programming language or specification language dedicated to a particular problem domain
![Page 32: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/32.jpg)
Model Driven Architecture
![Page 33: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/33.jpg)
![Page 34: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/34.jpg)
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.
![Page 35: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/35.jpg)
Benefits of MDA
• Quality • Consistency • A single point of knowledge • More design time • Design decisions that stand out
![Page 36: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/36.jpg)
Software Factories
![Page 37: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/37.jpg)
Industrializing Software Development
![Page 38: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/38.jpg)
Software Factory
![Page 39: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/39.jpg)
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!
![Page 40: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/40.jpg)
Code Generation Skills
• Text Templates • Regular Expressions • XML Parsing • Reflection
![Page 41: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/41.jpg)
Show me the tools
![Page 42: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/42.jpg)
Available tools
• StringBuilder • CodeSnippets • XSLT • Reflection Emit • EnvDTE • CodeDom • T4 • … (Python, Ruby,
![Page 43: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/43.jpg)
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.
![Page 44: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/44.jpg)
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!
![Page 45: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/45.jpg)
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.
![Page 46: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/46.jpg)
Reflection Emit
It 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
![Page 47: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/47.jpg)
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.
![Page 48: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/48.jpg)
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.
![Page 49: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/49.jpg)
Text Template Transformation Toolkit
![Page 50: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/50.jpg)
How T4 Works ?
![Page 51: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/51.jpg)
T4 Tools
• Tangible T4 Editor for Visual Studio 2010 RTM adds IntelliSense and Syntax Coloring to T4 Text Templates.
• T4 Toolbox
![Page 52: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/52.jpg)
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.
![Page 53: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/53.jpg)
ReSharper 6.0 Code Generation
Create from Usage Generate Type Members Generate Type Constructor Generate Properties Generate Delegating Members Generate Formatting Members Generate Equality Members Implement/Override Methods
![Page 54: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/54.jpg)
Some advices
![Page 55: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/55.jpg)
Shall I Generate Interfaces?
![Page 56: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/56.jpg)
Shall I Generate Interfaces?
No !
![Page 57: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/57.jpg)
Shall I Unit Test generated code ?
• You shouldn’t unit test generated code. You may unit test generator instead.
![Page 58: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/58.jpg)
Real World Case Studies
![Page 59: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/59.jpg)
Case 1: Client – Server Communication
Existing API
Generated Server Proxy
Generated Client Proxy
Communication channel
![Page 60: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/60.jpg)
Case 2: API Wrapper Le
gacy
API
Adap
ter Exist
ing
Appl
icat
ion
![Page 61: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/61.jpg)
Case 3: Protocol Parser
![Page 62: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/62.jpg)
Case 4: Model Driven DAL
Partially generated DB
Generated Entities
Generated Repositories
Hand written services
XML Model
![Page 63: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/63.jpg)
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;
![Page 64: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/64.jpg)
Do try this !
• Code generation can make you flexible and responsive. Do try this at home. (Or at work)
![Page 65: NetWork - 15.10.2011 - Applied code generation in .NET](https://reader034.vdocuments.net/reader034/viewer/2022051609/548105edb4af9fd3158b6056/html5/thumbnails/65.jpg)