Download - By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com
![Page 1: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/1.jpg)
Internal Domain-specific Languages in
C#By Chad MyersLosTechies.com
![Page 2: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/2.jpg)
Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com
Introduction
![Page 3: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/3.jpg)
Lambda Functions◦ Inline and Multiline ◦ Block
Closures Type, List, and Dictionary Initializers Type Inferrence Basics of Expression Trees Static Reflection
Quick Primer on Advanced C# 3.0
![Page 4: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/4.jpg)
What? – “Language within a Language” for targeted, specific purpose
Why? – To enhance the usability of an API designed and highly targeted for a specific purpose
Who? – Martin Fowler, Jeremy Miller, Ayende Rahien, Jay Fields
When? – In .NET, primarily since C# 3.0 (mini-DSLs in .NET have existed since 1.1)
How? – Attend this Workshop! Read Fowler, Miller, Rahien, Fields
Internal DSL Introduction
![Page 5: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/5.jpg)
Bending an existing language and compiler Unconventional – but still disciplined – use
of language techniques (Language Oriented Programming)
Focus on Flow, Comprehension and Accelerated Use of Finished Product
Should Be (Outwardly…):◦ Expressive◦ Efficient/Productive◦ Conventional (in the context of the domain)◦ Discoverable, Flowing
Internal DSL Concepts
![Page 6: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/6.jpg)
Small DSLs◦ Problem/Purpose Identification◦ Pattern Application◦ Refactoring
Large DSLs◦ Problem/Purpose Identification◦ Major component Identification and Minimal
Design◦ Pattern Application◦ Refactoring
Internal DSL Strategies
![Page 7: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/7.jpg)
End Result◦ Generative◦ Non-Generative
Internal Structure◦ Semantic Model (Internal & External)◦ No Model
Internal DSL Form and Type
![Page 8: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/8.jpg)
Before we get into the meat, any questions?
Questions Here
![Page 9: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/9.jpg)
Building Block Concepts◦ Context Variables and State Management◦ Generic Type Specifiers
Building Block Patterns◦ Method Chaining◦ Nested Function◦ Nested Closure◦ Literal Type/Collection Expression (Initializers)◦ Dynamic Reception (Extension Methods)◦ Parse Tree Manipulation (Expression Trees)
Internal DSL Patterns
![Page 10: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/10.jpg)
Structural Patterns◦ Expression Builder◦ Object Scoping◦ Delegation (Nested Closures Revisited)◦ Convention
Internal DSL Patterns
![Page 11: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/11.jpg)
Start with Host Object Return Host Object from Each Call Modifies State or Performs an Action Purpose:
◦ Assembly/Population of an Object or Objects◦ Allows for in-line or multi-line statements◦ Provides clear path for consumers
Examples:◦ System.DateTime◦ System.String
Method Chaining Pattern
![Page 12: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/12.jpg)
Small, Focused, Deterministic Encapsulates Frequent Tasks Helps Keep Flow Helps Enhance Language Examples:
◦ DateTime.Parse(), *.Parse()◦ TimeSpan.FromMinutes
Nested Function Pattern
![Page 13: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/13.jpg)
Delegation Scoping Physical Separation of Tasks Deterministic Resolution of Task Solves “Rathole” Problem Examples:
◦ Registry.Scan (StructureMap)◦ PersistenceModel.ForEach (Fluent Nhibernate)
Nested Closure Pattern
![Page 14: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/14.jpg)
Type/Collection/Dictionary Initializers Can Replace Method Chaining in Many
Circumstances Can be More Expressive, in Less Spaces Examples:
◦ MsSqlConifgurationTester (Fluent Nhibernate)
Literal Type Expression Pattern
![Page 15: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/15.jpg)
In C# 3.0, this means Extension Methods In C# 4.0, much, much more Attaching Different Behavior to Existing API Extensibility Point for Existing DSLs Allows for Different Grammars for Same API Examples:
◦ XmlExtensions (Fluent Nhibernate)◦ Specification Extensions (Fluent Nhibernate)
Dynamic Reception Pattern
![Page 16: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/16.jpg)
In C#, this means Expression Trees Useful for Static Reflection Can Compose Expressions Examples:
◦ ClassMap<T> (Fluent Nhibernate)◦ EntityQuery stuff (Fluent Nhibernate)
Parse Tree Manipulation Pattern
![Page 17: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/17.jpg)
Any questions on building block patterns?
Questions
![Page 18: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/18.jpg)
State Management Progressive Interfaces The “Rathole” Problem Examples:
◦ CascadeExpression (Fluent Nhibernate)◦ ManyToManyPart (Fluent Nhibernate)◦ ActionLinkExpression (Dovetail)◦ ExpressionBase (Dovetail)
Expression Builder Pattern
![Page 19: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/19.jpg)
Can be used directly, or serves as base class
Contains starter methods Launching point for other patterns Examples:
◦ Registry (StructureMap)◦ PersistenceModel (Fluent Nhibernate)
Object Scoping Pattern
![Page 20: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/20.jpg)
Can also be structural Examples:
◦ ObjectFactory.Initialize (StructureMap)
Delegation (Nested Closures)
![Page 21: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/21.jpg)
Discovery of types Identification of interesting types Application Examples:
◦ ITypeScanner (StructureMap)◦ AutoPersistenceModel (Fluent Nhibernate)
Conventions
![Page 22: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/22.jpg)
Any questions on structural patterns?
Questions
![Page 23: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/23.jpg)
Build a model-based generative Internal DSL for writing a fairy tale story
End result: Print the story to the debug window
Demonstrate as many patterns as possible in one DSL
In Practice: Fairy Tale Builder
![Page 24: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/24.jpg)
FairyTale object Story Parts (Intro, focus, plot, ending) Rendering
Fairy Tale Model
![Page 25: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/25.jpg)
Delegation -- Nested Closure Expression Builder in closure Method Chaining with Generic Type
Specifiers Literal Type Expressions Parse Tree Manipulation Dynamic Reception
Fairy Tale Builder
![Page 26: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/26.jpg)
Any questions on Fairy Tale Builder?
Questions
![Page 27: By Chad Myers LosTechies.com. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649cd95503460f949a33d8/html5/thumbnails/27.jpg)
Chad [email protected]
http://chadmyers.lostechies.com
Martin Fowler’s DSL WIPhttp://martinfowler.com/dslwip
Closing