Exploring C# DSLs:LINQ, Fluent Interfaces and     Expression Trees   Rasmus Kromann-Larsen
Me• Freelance .NET Developer• Core Group Member  – Copenhagen .NET User Group  – Aarhus .NET User Group• JetBrains Academy...
Agenda• Domain Specific Languages• Static Reflection• Code
Domain Specific Languages• Language targeted at a specific problem• Goals  – Expressiveness / Productivity  – Declarative ...
SELECT ID, LastNameFROM Employees eWHERE EXISTS (    SELECT *    FROM Orders o    WHERE e.ID = o.EmployeeID    AND OrderDa...
(?:(?:rn)?[ t])*(?:(?:(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"...
Talk Focus• Internal C# DSLs  – Less ramp-up  – Automatic tooling support  – Emergent in many new libraries• Developer ben...
[Subject("Making a customer preferred")]public class when_a_regular_customer_is_made_preferred{    static Customer _custom...
people.Where(x => x.Age < 50)     from x in people     where x.Age < 50     select x
LINQ• Most(?) successful C# DSL• Depending on C# 3.0 features  – Anonymous Types  – Extension Methods  – Expression Trees•...
LINQpeople.Where(x => x.Age < 50)     from x in people     where x.Age < 50     select x
Expression Trees
Expression Trees• Extracting AST-like structure from lambda  expressions• Expression<TDelegate>
Static Reflection• Statically typed reference to code element• No magic strings• Tool support / Refactorability• typeof(Pe...
GetProperty<Person>(x => x.Name)
GetPropertyNamepublic string GetPropertyName(Expression body){    // Unwrap boxing    if (body is UnaryExpression)    {   ...
GetProperty<Person>(x => x.Friend.Name)
Object Printing
Recap• We use DSLs almost every day• Expression Trees are not (that) dangerous• It doesn’t have to be hard to write a DSL!
More Information?• Domain Specific Languages by Martin Fowler• Open Source Projects  – Fubu  – Most IoC containers  – Auto...
Questions? Rasmus Kromann-Larsen       @rasmusklrasmus@kromann-larsen.dk    www.rasmuskl.dk
Upcoming SlideShare
Loading in …5
×

Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees

2,522 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,522
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees

  1. 1. Exploring C# DSLs:LINQ, Fluent Interfaces and Expression Trees Rasmus Kromann-Larsen
  2. 2. Me• Freelance .NET Developer• Core Group Member – Copenhagen .NET User Group – Aarhus .NET User Group• JetBrains Academy Expert
  3. 3. Agenda• Domain Specific Languages• Static Reflection• Code
  4. 4. Domain Specific Languages• Language targeted at a specific problem• Goals – Expressiveness / Productivity – Declarative over Imperative – Communication• External vs Internal
  5. 5. SELECT ID, LastNameFROM Employees eWHERE EXISTS ( SELECT * FROM Orders o WHERE e.ID = o.EmployeeID AND OrderDate = 9/5/11)
  6. 6. (?:(?:rn)?[ t])*(?:(?:(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"(?:(?: rn)?[ t])*)(?:.(?:(?:rn)?[t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*))*@(?:(?:rn)?[t])*(?:[^()<>@,;:".[] 000-... snip ..."()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)?[ t]) *))*@(?:(?:rn)?[t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?: .(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[t])*))*>(?:( ?:rn)?[ t])*))*)?;s*)
  7. 7. Talk Focus• Internal C# DSLs – Less ramp-up – Automatic tooling support – Emergent in many new libraries• Developer benefits > non-dev communication
  8. 8. [Subject("Making a customer preferred")]public class when_a_regular_customer_is_made_preferred{ static Customer _customer; Establish context = () => _customer = new Customer(); Because of = () => _customer.MakePreferred(); It should_mark_the_customer_as_preferred = () => _customer.IsPreferred.ShouldBeTrue();}
  9. 9. people.Where(x => x.Age < 50) from x in people where x.Age < 50 select x
  10. 10. LINQ• Most(?) successful C# DSL• Depending on C# 3.0 features – Anonymous Types – Extension Methods – Expression Trees• Great DSL building blocks
  11. 11. LINQpeople.Where(x => x.Age < 50) from x in people where x.Age < 50 select x
  12. 12. Expression Trees
  13. 13. Expression Trees• Extracting AST-like structure from lambda expressions• Expression<TDelegate>
  14. 14. Static Reflection• Statically typed reference to code element• No magic strings• Tool support / Refactorability• typeof(Person).GetProperty(”Name”); vs• GetProperty<Person>(x => x.Name);
  15. 15. GetProperty<Person>(x => x.Name)
  16. 16. GetPropertyNamepublic string GetPropertyName(Expression body){ // Unwrap boxing if (body is UnaryExpression) { body = ((UnaryExpression) body).Operand; } var memberExpression = body as MemberExpression; if (memberExpression == null) { throw new Exception("Descriptive exception message."); } return memberExpression.Member.Name;}
  17. 17. GetProperty<Person>(x => x.Friend.Name)
  18. 18. Object Printing
  19. 19. Recap• We use DSLs almost every day• Expression Trees are not (that) dangerous• It doesn’t have to be hard to write a DSL!
  20. 20. More Information?• Domain Specific Languages by Martin Fowler• Open Source Projects – Fubu – Most IoC containers – AutoMapper – AutoFixture – Fluent Nhibernate ... and many more.
  21. 21. Questions? Rasmus Kromann-Larsen @rasmusklrasmus@kromann-larsen.dk www.rasmuskl.dk

×