ebook img

Pro DLR in .NET 4 PDF

314 Pages·2010·5.216 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Pro DLR in .NET 4

Pro DLR in .NET 4 (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:3) (cid:3) (cid:3)(cid:2) Chaur Wu Pro DLR in .NET 4 Copyright © 2010 by Chaur Wu All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3066-3 ISBN-13 (electronic): 978-1-4302-3067-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Jonathan Gennick Technical Reviewer: Scott Isaacs Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Jennifer L. Blackwell Copy Editor: Sharon Terdeman Compositor: Bytheway Publishing Services Indexer: Brenda Miller Artist: Integra Software Services Pvt. Ltd. Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. I want to dedicate this book to Sarah, Everett, Cedric, and Chiachi (cid:3) CONTENTS AT A GLANCE Contents at a Glance (cid:3) About the Author..................................................................................................xiv (cid:3) About the Technical Reviewer..............................................................................xv (cid:3) Acknowledgments...............................................................................................xvi (cid:3) Introduction........................................................................................................xvii PART 1 (cid:3) (cid:3) (cid:3) DLR Fundamentals............................................................................1 (cid:3) Chapter 1: Introduction to DLR...............................................................................3 (cid:3) Chapter 2: DLR Expression....................................................................................27 (cid:3) Chapter 3: Late Binding and Caching....................................................................65 (cid:3) Chapter 4: Late Binding and Interoperability........................................................87 (cid:3) Chapter 5: Dynamic Objects................................................................................109 (cid:3) Chapter 6: DLR Hosting API.................................................................................133 PART 2 (cid:3) (cid:3) (cid:3) Applying the DLR...........................................................................163 (cid:3) Chapter 7: DLR and Aspect-Oriented Programming...........................................165 (cid:3) Chapter 8: Metaprogramming.............................................................................185 (cid:3) Chapter 9: Stitch — A DSL for Hosting Languages.............................................211 (cid:3) Chapter 10: Application Scripting.......................................................................239 (cid:3) Chapter 11: DLR in Silverlight.............................................................................251 (cid:3) Chapter 12: Dynamic Languages on JVM...........................................................275 (cid:3) Index...................................................................................................................297 iv (cid:3) CONTENTS Contents (cid:3) About the Author..................................................................................................xiv (cid:3) About the Technical Reviewer..............................................................................xv (cid:3) Acknowledgments...............................................................................................xvi (cid:3) Introduction........................................................................................................xvii PART 1 (cid:3) (cid:3) (cid:3) DLR Fundamentals............................................................................1 (cid:3) Chapter 1: Introduction to DLR...............................................................................3 Setting Up Code Examples.................................................................................................4 Software Requirements............................................................................................................................4 Installing the DLR, IronPython, and IronRuby...........................................................................................5 Hello World Examples........................................................................................................6 Implementing REPL for the Hello Language.............................................................................................9 Practical Uses for the DLR.................................................................................................9 Application Scripting and DSL................................................................................................................10 XML Builder............................................................................................................................................10 Aspect-Oriented Programming...............................................................................................................11 Runtime...........................................................................................................................13 Runtime vs. Run Time.............................................................................................................................14 Run Time vs. Compile Time....................................................................................................................14 Dynamic vs. Static...........................................................................................................16 Dynamic Typing......................................................................................................................................17 Dynamic Dispatch...................................................................................................................................20 Language.........................................................................................................................21 Programming Languages in Practice...............................................................................22 v (cid:3) CONTENTS Putting It Together...........................................................................................................24 Summary.........................................................................................................................25 (cid:3) Chapter 2: DLR Expression....................................................................................27 DLR Expression as a Language........................................................................................27 Code as Data..........................................................................................................................................28 A Common Denominator like CLR...........................................................................................................28 Concrete Syntax and Serialization..........................................................................................................29 Expressions vs. Statements.............................................................................................29 Expression Type and Kind................................................................................................31 Binary Expressions..........................................................................................................33 Flow Control Expressions................................................................................................36 If-Then-Else Expressions........................................................................................................................37 Switch Expressions................................................................................................................................38 Scopes and Name Binding...............................................................................................39 Lexical vs. Dynamic Scope.....................................................................................................................40 BlockExpression and Lexical Scoping....................................................................................................41 Lambda Expressions and Closure....................................................................................44 The GotoExpression Class................................................................................................46 While Loops.....................................................................................................................48 Dynamic Expressions.......................................................................................................49 Index Expressions............................................................................................................52 Expression Abstraction, Reduction and Extension...........................................................53 Immutability and the Visitor Pattern................................................................................55 Visitor Pattern in General........................................................................................................................56 Visitor Pattern in DLR Expression...........................................................................................................57 Expression Visitor Examples...................................................................................................................59 Summary.........................................................................................................................63 vi (cid:3) CONTENTS (cid:3) Chapter 3: Late Binding and Caching....................................................................65 Binding.............................................................................................................................65 Call Sites and Early Binding.............................................................................................66 Call Sites and Late Binding..............................................................................................67 DLR Binders.....................................................................................................................68 Set Up Code Examples.....................................................................................................69 Making a Debug Build of the DLR for .NET 2.0.......................................................................................69 Developing for Both .NET 2.0 and .NET 4.0............................................................................................69 The CallSiteBinder Class..................................................................................................71 DLR Call Sites..................................................................................................................72 Binding Restrictions and Rules........................................................................................73 Checking Binding Rules in Debug Mode..........................................................................75 Caching............................................................................................................................78 Three Cache Levels................................................................................................................................78 Late-Binding Context..............................................................................................................................80 L0 Cache Example..................................................................................................................................82 L1 Cache Example..................................................................................................................................83 L2 Cache Example..................................................................................................................................84 Creating Canonical Binders.............................................................................................86 Summary.........................................................................................................................86 (cid:3) Chapter 4: Late Binding and Interoperability........................................................87 Language Interoperability................................................................................................87 Static and Dynamic Objects.............................................................................................91 Late-Binding Logic in Two Places....................................................................................92 Late Binding Logic in Binders.................................................................................................................92 Late-Binding Logic in Dynamic Objects..................................................................................................94 Late-Bound Actions.........................................................................................................94 vii (cid:3) CONTENTS Examples................................................................................................................................................94 Common Type System............................................................................................................................96 Class Hierarchy of Binders.....................................................................................................................99 Implement a Custom Binder Class.................................................................................100 Interoperability Protocol................................................................................................102 Summary.......................................................................................................................106 (cid:3) Chapter 5: Dynamic Objects................................................................................109 What is a Dynamic Object?............................................................................................109 IDynamicMetaObjectProvider Interface.........................................................................111 Dynamic Meta Objects...................................................................................................112 DynamicMetaObject and Binding Logic................................................................................................113 DynamicMetaObject and Binding Result..............................................................................................114 Interoperability...............................................................................................................115 DynamicObject Class.....................................................................................................119 XML Builder...................................................................................................................122 Summary.......................................................................................................................130 (cid:3) Chapter 6: DLR Hosting API.................................................................................133 Life Without the DLR Hosting API...................................................................................133 Using a Static Language’s Code in Another Static Language...............................................................134 Using a Static Language’s Code in a Dynamic Language.....................................................................134 Using a Dynamic Language’s Code in a Static Language.....................................................................135 Using a Dynamic Language’s Code in Another Dynamic Language.....................................................136 Overview of the DLR Hosting API...................................................................................136 Major Classes in the API.......................................................................................................................137 The Tale of Two APIs............................................................................................................................138 The DLR Hosting API in Relation to Binders and Dynamic Objects.......................................................139 Using Script Runtime to Execute Code..........................................................................139 viii (cid:3) CONTENTS Configuring the Languages You Want to Speak.............................................................140 Configuring Script Runtime Declaratively.............................................................................................141 Configuring Script Runtime Programmatically.....................................................................................142 Scripting an Object Model.............................................................................................143 Script Scopes.................................................................................................................145 Value and Reference Variables......................................................................................146 Global Scope and Variable Passing......................................................................................................147 Language Neutral Scope and Variable Passing....................................................................................148 Level Two Use of the DLR Hosting API...........................................................................150 Compiling Code.....................................................................................................................................150 Loading Assemblies into Script Runtime..............................................................................................152 Creating Python Class Instances Using Object Operations...................................................................153 Level Three Use of the DLR Hosting API........................................................................154 Script Host............................................................................................................................................154 Object Operations.................................................................................................................................156 Remote Script Runtime........................................................................................................................156 .NET Remoting Quick Tour.............................................................................................158 Running Script Runtime in a Separate Process.............................................................159 Summary.......................................................................................................................161 PART 2 (cid:3) (cid:3) (cid:3) Applying the DLR...........................................................................163 (cid:3) Chapter 7: DLR and Aspect-Oriented Programming...........................................165 Aspect-Oriented Programming......................................................................................165 Cross-Cutting Concerns........................................................................................................................165 Advice, Join Points, and Pointcuts.......................................................................................................166 An Example...........................................................................................................................................167 A Test Run............................................................................................................................................170 AOP for Dynamic Objects...............................................................................................171 Understanding the Framework.............................................................................................................171 ix (cid:3) CONTENTS Implementing the Framework..............................................................................................................172 Integration with Spring.NET AOP...................................................................................174 Getting the AOP Advisors......................................................................................................................175 Implementing Advice............................................................................................................................177 Applying Advice....................................................................................................................................178 Cutting Across Dynamic and Static Objects.........................................................................................180 Summary.......................................................................................................................183 (cid:3) Chapter 8: Metaprogramming.............................................................................185 Overview of Metaprogramming.....................................................................................185 Changing Class Definitions............................................................................................186 Ruby.....................................................................................................................................................187 Python...................................................................................................................................................189 DLR.......................................................................................................................................................191 LINQ Query Provider.......................................................................................................196 Understanding the End Goal.................................................................................................................196 Implementing the Query Class..............................................................................................................196 Implementing the QueryProvider Class................................................................................................198 Implementing QueryExpressionVisitor..................................................................................................200 Data Access...................................................................................................................201 Static Data Access................................................................................................................................202 Dynamic Data Access...........................................................................................................................204 Generated Data Access........................................................................................................................206 Summary.......................................................................................................................210 (cid:3) Chapter 9: Stitch — A DSL for Hosting Languages.............................................211 The Need for Stitch........................................................................................................211 Syntax of the Stitch Language.......................................................................................212 Requirements for the Example......................................................................................214 Software Requirements........................................................................................................................214 x

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.