4. Domain Specific Languages
• Language targeted at a specific problem
• Goals
– Expressiveness / Productivity
– Declarative over Imperative
– Communication
• External vs Internal
5. SELECT ID, LastName
FROM Employees e
WHERE EXISTS (
SELECT *
FROM Orders o
WHERE e.ID = o.EmployeeID
AND OrderDate = '9/5/11')
7. Talk Focus
• Internal C# DSLs
– Less ramp-up
– Automatic tooling support
– Emergent in many new libraries
• Developer benefits > non-dev communication
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();
}
16. GetPropertyName
public 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;
}
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. More Information?
• Domain Specific Languages by Martin Fowler
• Open Source Projects
– Fubu
– Most IoC containers
– AutoMapper
– AutoFixture
– Fluent Nhibernate
... and many more.