Sander Hoogendoorn, village idiot, Capgemini 1
SANDER HOOGENDOORN Capgemini Principal technology officer Global agile thought leader Chief architect Accelerated Delivery Platform (ADP) Other Author books and articles Appreciated speaker at international conferences Microsoft Partner Advisory Council.NET Capping IT Off Blog Column Software Release Magazine Columns SDN Magazine Editorial board Software Release Magazine Editorial board Tijdschrift IT Management (TiTM) Advisory board @Portunity Web www.sanderhoogendoorn.com www.smartusecase.com Twitter: @aahoogendoorn LinkedIn: aahoogendoorn 2
AGENDA Extension methods What are extension methods anyway? Which problem do they solve? How to define an extension method How do they work? How are they implemented? Object oriented principles What is LSP? Do extension methods break LSP? What about OCP? Some usages Extension method levels Extending frameworks Extension method anti-patterns 3
HOW COMPLECITY IS KILLING US 4
THE GARTNER HYPE CURVE 5
PRODUCTIVITY 6
PRODUCTIVITY? 7
THE IT MANAGER IT has become a major roadblock for business agility 8
9
SIMPLICITY? 10
SIMPLICITY? 11
SIMPLICITY? 12
13
Extension methods? 14
WHAT ARE ANYWAY? An often overlooked language feature 15
WHAT ARE ANYWAY? When designing LINQ Implement standard query operators for LINQ Add functionality to IEnumerable and IEnumerable<T> types But without touching the original types! Enter extension methods Any type implementing these will now have OrderBy, GroupBy etc. 16
WHAT ARE ANYWAY? 17
WTF? 18
WHAT PROBLEM DO THEY SOLVE? 19
WHAT S THE PROBLEM ANYWAY? Problem You need to extend an existing type and add functionality to it Options 1. Implement the functionality on the type itself e.g. in a partial class 2. Inherit from the type and add the functionality there override and new? 3. Apply aggregation instead of inheritance 4. Create a (static) helper class and add the functionality there 5. Create the functionality in extension methods 20
OVERRIDING USING NEW KEYWORD Demo 21
USING INHERITANCE Demo 22
USING UTILITY Demo 23
HOW TO DEFINE AN EXTENSION METHOD 24
EXTENSIONS: BEFORE AND AFTER 25
. WTF? Extension methods Enable you to "add" methods to existing types, without derivation Call as if they are methods on the extended type Full IntelliSense support For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type 26
DEFINING AN EXTENSION METHOD Rules for defining extension method Defined in a non-generic non-nested static class Method is static First parameter preceded by the instance parameter this No other parameters (ref, out, etc ) are allowed with modifier this The instance parameter cannot be a pointer Must be public, internal or private In a namespace which is in scope 27
MY FIRST EXTENSION METHOD Demo string.isemail 28
SO HOW ARE IMPLEMENTED? Some MSIL 29
SO HOW ARE IMPLEMENTED? 30
SO HOW ARE IMPLEMENTED? 31
LSP AND OCP Facts of life? 32
LISKOV S SUBSTITUTION PRINCIPLE If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behaviour of P is unchanged when o1 is substituted for o2 then S is a subtype of T. 33
LSP AND OCP Liskov Substitution Principle (LSP) If S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program. Barbara Liskov 1994. Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T. Open Closed Principle (OCP) Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Betrand Meyer, 1988. 34
BREAKING LSP TWICE Demo 35
LSP AND OCP How do LSP and OCP relate to extension methods? Defined as a static member on a non-generic, static class Not defined on extended class itself Check your MSIL for consequences Thus Extension methods can only get to public properties of extended class And Extension method can not reach extended class internals Concluding Extension methods do not break LSP Extension methods do not break OCP So nice implementation! 36
EXTENSTION METHOD INVOCATION Resolving the order of precedence 37
(EXTENSION) METHOD INVOCATION First try regular method invocation expr.identifier() expr.identifier(args) expr.identifier<typeargs>() expr.identifier<typeargs(args) Only if none resolves find eligble extension methods C.identifier(expr) C.identifier(expr, args) C.identifier<typeargs>(expr) C.identifier<typeargs>(expr, args) Eligble if implicit identity, reference or boxing conversion exists from expr to first parameter of identifier 38
EXTENSION METHOD INVOCATION Demo 39
LEVELS OF 40
TYPES OF Level one Rewriting static framework methods as instance extension methods Format(this string s) Level two Creating new extension methods to solve common problems IsNullOrEmpty(this string s) Level three Using extension methods to embed business and validation logic IsEmail(this string s) Rather use value objects 41
TYPES OF Level four Method chaining Fluent interfaces 5.Sqrt().Power(3) Level five Extending framework functionality DomainObject HtmlHelper Replacing framework functionality Splitting basic from specialized scenarios 42
SIMPLE METHOD CHAINING Demo 43
Extending frameworks 44
EXTENDING EXISTING FRAMEWORKS - ADF 45
EXTENDING EXISTING FRAMEWORKS - ADF 46
EXTENDING EXISTING FRAMEWORKS - ADF 47
EXTENDING EXISTING FRAMEWORKS - CSLA 48
EXTENDING AND REPLACING 49
EXTENDING HTMLHELPER Demo ASP. NET MVC Descriptor pattern 50
EXTENSION METHOD ANTI-PATTERNS Do s and don ts 51
EXTENSION METHOD ANTI-PATTERNS 52
EXTENSION METHOD ANTI-PATTERNS 53
EXTENSION METHOD ANTI-PATTERNS 54
ANTI-PATTERNS 55
SENDING EMAIL? Demo 56
TAKEAWAYS 57
TAKEAWAYS Extension methos In general, you will call extension methods far more than implement your own Don t forget to include the namespace Implement them sparsely Be careful with additional dependencies!! Use extension methods to extend frameworks that do not allow you to extend their types Don t mistake extension methods for value objects, just because they have the same underlying base type Mind your namespaces! 58
WWW.SMARTUSECASE.COM 59
REFERENCES AND QUESTIONS sander.hoogendoorn@capgemini.com www.accelerateddeliveryplatform.com www.smartusecase.com www.sanderhoogendoorn.com sander.hoogendoorn@capgemini.com aahoogendoorn@gmail.com LinkedIn: aahoogendoorn Twitter: @aahoogendoorn 60