SLIDE 1 DYNAMIC: DON’T BE AFRAID
Hadi Hariri JetBrains
SLIDE 2 The What, the Why, the How
Agenda
SLIDE 3 A tale as old as time…
A Tale as Old as Time...
SLIDE 4 Static vs Dynamic
Static vs Dynamic
SLIDE 5
Blog Posts Discussion Threads Blog Posts and more blog posts
SLIDE 6
- Types can be implicit or explicit (var)
- Compiler Safety
- Early Binding
In the Static World
SLIDE 7
- Types defined at runtime
- No Compiler (Usually)
- Late Binding
- Interpreted (Not always)
In a Dynamic World
SLIDE 8
What Dynamic Developers think of Static Developers…
SLIDE 9
I need my compiler!
SLIDE 10
What Static Developers think of Dynamic Developers…
SLIDE 11 http://nimblepros.com/products/software-craftsmanship-2012-calendar.aspx
SLIDE 12
"You cannot build serious business applications in dynamic languages"
SLIDE 13
SLIDE 14
SLIDE 15
SLIDE 16
They both have Good and Bad Things
SLIDE 17
DLR & C# 4
SLIDE 18
- dynamic keyword
- Classes/Binders and interfaces to work with
dynamic types
C# added...
SLIDE 19 Hosting API Debugging API Interop Binders Dynamic Objects Call-Site Caching Expressions Expression Compiler / Interpreter IL Code Generator
DLR added…
SLIDE 20 DLR IronRuby IronPython C# VB.NET Bindings .NET Ruby Python Office
The Big Picture
SLIDE 21 http://www.flickr.com/photos/bitjungle
SLIDE 22 GET YOUR STINKING DYNAMIC TYPES OFF OF MY STATIC LANGUAGE
SLIDE 23
IS THERE A NEED?
SLIDE 24
Readability
SLIDE 25
SLIDE 26
Interoperability
SLIDE 27 Interoperability with other languages
- IronPython
- Interpreted
- Can be compiled
- IronRuby
- Interpreted
- Your own language
SLIDE 28
SLIDE 29
SLIDE 30
SLIDE 31 DEMO
TALKING RUBY
SLIDE 32 Interoperability
- Talking to COM
- Need a type-library beforehand
- Use Method Invocation
SLIDE 33 DEMO
TALKING COM
SLIDE 34
The Case of the DTO
SLIDE 35
Creating Dynamic Objects in C#
SLIDE 36 Options
- ExpandObject
- DynamicObject
- IDynamicMetaObjectProvider
SLIDE 37 Expando Object
- Built-in Dynamic Object. Works out of the box
- Benefits over Dictionary
- More Fluent
- Support for Methods
- Supports Hierarchies
- Implements INotifyPropertyChanged
- Limitations
- Index Access
SLIDE 38 DEMO
ON THE FLY: BASICS OF DYNAMIC
SLIDE 40 DynamicObject
- Moving Beyond an Expando
- Built-in class which implements
IDynamicMetaObjectProvider
- Allows easy creation of Dynamic types
SLIDE 41 DEMO
MVC – VIEWBAG, DYNAMICOBJECTSIMPLE
SLIDE 42 IDynamicMetaObjectProvider
- Meta Object that performs binding
- Allows decoupling from class
- Uses DLR Expressions
- Returns DynamicObject
SLIDE 43 DEMO
DYNAMICPROVIDER
SLIDE 44
Undetermined API
SLIDE 45 Aspects of MetaProgramming
- Adding / Removing Methods
- Creating Instance Methods
- Creating Static / Class Methods
- Query Classes
SLIDE 46
SLIDE 47 DEMO
DYNAMICMETHODMISSING – SIMPLE DATA
SLIDE 48
Consuming the ever-changing
SLIDE 49 DEMO
CONSUMINGJSON
SLIDE 50
DSL's and Fluent API's
SLIDE 51
A QUICK LOOK UNDER THE HOOD…
SLIDE 52 The backbone of dynamic support
DLR
Language Semantics via DLR EXpression Define Late Binding Logic
SLIDE 53 DLR Expression
- Superset of Linq.Expression
- Common to multiple Languages
- DLR Expression is to DLR Languages what IL
is to CLR languages
C#
VB.NET
IL CLR IronPython IronRuby DLR Expression DLR Runtime
SLIDE 54 Late Binding
- We only know the types at runtime
- We have to figure out how to call those
methods at runtime
- It’s not embedded in the “IL”
- It’s potentially slower
SLIDE 55 Late Binding
- Using Binders and Call Sites
- Using Dynamic Expression (uses former
internally)
SLIDE 57 DEMO
DYNAMICCONVERSION
SLIDE 58
SUMMING UP…
SLIDE 59 The Disadvantages
- There is no compile type-checking*
- Potentially slower (even with caching)
- There is no Intellisense*
* Partially incorrect – It’s about the tooling
SLIDE 60
Reasons to not not use dynamic
SLIDE 61 Reasons to not not use dynamic
- There’s no compiler
- There’s no intellisense (Emphasis on Unit Tests)
- You shouldn’t mix dynamic and static languages
SLIDE 62
Reasons to use dynamic
SLIDE 63 Reasons to use Dynamic
- Interoperability
- COM
- Consuming Dynamic Languages
- Ruby
- JavaScript
- Fluent API’s and DSL
- Consuming the *unknown*
- Dynamic Structures
- Avoiding unnecessary “class explosion”