1 / 27

Internal Domain-specific Languages in C#

Internal Domain-specific Languages in C#. By Chad Myers LosTechies.com. Introduction. Housekeeping Stuff Meet the Presenter ALT.NET LosTechies.com. Quick Primer on Advanced C# 3.0. Lambda Functions Inline and Multiline Block Closures Type, List, and Dictionary Initializers

medea
Télécharger la présentation

Internal Domain-specific Languages in C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internal Domain-specific Languages in C# By Chad MyersLosTechies.com

  2. Introduction • Housekeeping Stuff • Meet the Presenter • ALT.NET • LosTechies.com

  3. Quick Primer on Advanced C# 3.0 • Lambda Functions • Inline and Multiline • Block • Closures • Type, List, and Dictionary Initializers • Type Inferrence • Basics of Expression Trees • Static Reflection

  4. Internal DSL Introduction • What? – “Language within a Language” for targeted, specific purpose • Why? –Toenhance 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

  5. Internal DSL Concepts • 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

  6. Internal DSL Strategies • Small DSLs • Problem/Purpose Identification • Pattern Application • Refactoring • Large DSLs • Problem/Purpose Identification • Major component Identification and Minimal Design • Pattern Application • Refactoring

  7. Internal DSL Form and Type • End Result • Generative • Non-Generative • Internal Structure • Semantic Model (Internal & External) • No Model

  8. Questions Here • Before we get into the meat, any questions?

  9. Internal DSL Patterns • 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)

  10. Internal DSL Patterns • Structural Patterns • Expression Builder • Object Scoping • Delegation (Nested Closures Revisited) • Convention

  11. Method Chaining Pattern • 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

  12. Nested Function Pattern • Small, Focused, Deterministic • Encapsulates Frequent Tasks • Helps Keep Flow • Helps Enhance Language • Examples: • DateTime.Parse(), *.Parse() • TimeSpan.FromMinutes

  13. Nested Closure Pattern • Delegation • Scoping • Physical Separation of Tasks • Deterministic Resolution of Task • Solves “Rathole” Problem • Examples: • Registry.Scan (StructureMap) • PersistenceModel.ForEach (Fluent Nhibernate)

  14. Literal Type Expression Pattern • Type/Collection/Dictionary Initializers • Can Replace Method Chaining in Many Circumstances • Can be More Expressive, in Less Spaces • Examples: • MsSqlConifgurationTester (Fluent Nhibernate)

  15. Dynamic Reception Pattern • 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)

  16. Parse Tree Manipulation Pattern • In C#, this means Expression Trees • Useful for Static Reflection • Can Compose Expressions • Examples: • ClassMap<T> (Fluent Nhibernate) • EntityQuery stuff (Fluent Nhibernate)

  17. Questions • Any questions on building block patterns?

  18. Expression Builder Pattern • State Management • Progressive Interfaces • The “Rathole” Problem • Examples: • CascadeExpression (Fluent Nhibernate) • ManyToManyPart (Fluent Nhibernate) • ActionLinkExpression (Dovetail) • ExpressionBase (Dovetail)

  19. Object Scoping Pattern • Can be used directly, or serves as base class • Contains starter methods • Launching point for other patterns • Examples: • Registry (StructureMap) • PersistenceModel (Fluent Nhibernate)

  20. Delegation (Nested Closures) • Can also be structural • Examples: • ObjectFactory.Initialize (StructureMap)

  21. Conventions • Discovery of types • Identification of interesting types • Application • Examples: • ITypeScanner (StructureMap) • AutoPersistenceModel (Fluent Nhibernate)

  22. Questions • Any questions on structural patterns?

  23. In Practice: Fairy Tale Builder • 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

  24. Fairy Tale Model • FairyTale object • Story Parts (Intro, focus, plot, ending) • Rendering

  25. Fairy Tale Builder • Delegation -- Nested Closure • Expression Builder in closure • Method Chaining with Generic Type Specifiers • Literal Type Expressions • Parse Tree Manipulation • Dynamic Reception

  26. Questions • Any questions on Fairy Tale Builder?

  27. Closing • Chad Myerschad@chadmyers.comhttp://chadmyers.lostechies.com • Martin Fowler’s DSL WIPhttp://martinfowler.com/dslwip

More Related